From e3137030784b80b85ee7870cda906f8800bd7c65 Mon Sep 17 00:00:00 2001 From: Tobias Glasmachers Date: Wed, 4 Dec 2024 16:26:25 +0100 Subject: [PATCH] fixed broken hoisting with included files --- package.json | 2 +- src/lang/parser/index.ts | 11 +++++++++++ src/lang/parser/parse_fn.ts | 6 +++++- src/lang/version.ts | 4 ++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a7d2bd6..64dd1af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tscript", - "version": "1.3.8", + "version": "1.3.9", "description": "", "main": "tscript.js", "scripts": { diff --git a/src/lang/parser/index.ts b/src/lang/parser/index.ts index 6605c5a..e06872b 100644 --- a/src/lang/parser/index.ts +++ b/src/lang/parser/index.ts @@ -48,6 +48,7 @@ export class Parser { line: 1, // one-based line number filename: null, // filename or null ch: 0, // zero-based character number within the line + totalpos: 0, // sequential character index respecting include order indent: [0], // stack of nested indentation widths errors: [], // list of errors, currently at most one impl: {}, // implementations of built-in functions @@ -125,6 +126,7 @@ export class Parser { pos: this.pos, line: this.line, ch: this.ch, + totalpos: this.totalpos, filename: this.filename, }; }, @@ -132,6 +134,7 @@ export class Parser { this.pos = where.pos; this.line = where.line; this.ch = where.ch; + this.totalpos = where.totalpos; this.filename = where.filename; }, indentation: function () { @@ -159,6 +162,7 @@ export class Parser { } this.pos++; this.ch++; + this.totalpos++; } }, skip: function () { @@ -168,21 +172,25 @@ export class Parser { if (c === "#") { this.pos++; this.ch++; + this.totalpos++; if (this.current() === "*") { this.pos++; this.ch++; + this.totalpos++; let star = false; while (this.good()) { if (this.current() === "\n") { this.pos++; this.line++; this.ch = 0; + this.totalpos++; star = false; continue; } if (star && this.current() === "#") { this.pos++; this.ch++; + this.totalpos++; break; } star = this.current() === "*"; @@ -193,6 +201,7 @@ export class Parser { while (this.good() && this.current() !== "\n") { this.pos++; this.ch++; + this.totalpos++; } } continue; @@ -205,6 +214,7 @@ export class Parser { lines.push(this.line); } else this.ch++; this.pos++; + this.totalpos++; } return lines; }, @@ -251,6 +261,7 @@ export class Parser { } } }; + console.log(program); // recursive compiler pass through the syntax tree function compilerPass(passname) { diff --git a/src/lang/parser/parse_fn.ts b/src/lang/parser/parse_fn.ts index ee26c80..ecfc4c7 100644 --- a/src/lang/parser/parse_fn.ts +++ b/src/lang/parser/parse_fn.ts @@ -21,15 +21,18 @@ import { TScript } from ".."; import { Typeid } from "../helpers/typeIds"; export function resolve_name(state, name, parent, errorname) { + console.log("[resolve_name]", name); // prepare a generic "not defined" error let error = "/name/ne-5"; let arg = [errorname, name]; let lookup = null; let pe: any = parent; while (pe) { + if (name === "colors") console.log(pe); // check name inside pe if (pe.hasOwnProperty("names") && pe.names.hasOwnProperty(name)) { let n = pe.names[name]; + console.log("n", n); // check whether a variable or function is accessible if ( @@ -40,6 +43,7 @@ export function resolve_name(state, name, parent, errorname) { ) { // find the context let context = get_context(pe); + console.log("context", context); if ( n.petype !== "variable" && context.petype === "global scope" @@ -78,7 +82,7 @@ export function resolve_name(state, name, parent, errorname) { n.petype !== "variable" || n.parent.petype === "type" || !n.hasOwnProperty("where") || - n.where.pos < state.pos + n.where.totalpos < state.totalpos ) return pe; } diff --git a/src/lang/version.ts b/src/lang/version.ts index 1363d95..5c1442d 100644 --- a/src/lang/version.ts +++ b/src/lang/version.ts @@ -5,8 +5,8 @@ export const Version = { major: package_json.version.split(".")[0], minor: package_json.version.split(".")[1], patch: package_json.version.split(".")[2], - day: 24, - month: 11, + day: 4, + month: 12, year: 2024, full: function () { let s =