Skip to content

Commit

Permalink
fixed broken hoisting with included files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Glasmachers committed Dec 4, 2024
1 parent 6fee76c commit e313703
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tscript",
"version": "1.3.8",
"version": "1.3.9",
"description": "",
"main": "tscript.js",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions src/lang/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,13 +126,15 @@ export class Parser {
pos: this.pos,
line: this.line,
ch: this.ch,
totalpos: this.totalpos,
filename: this.filename,
};
},
set: function (where) {
this.pos = where.pos;
this.line = where.line;
this.ch = where.ch;
this.totalpos = where.totalpos;
this.filename = where.filename;
},
indentation: function () {
Expand Down Expand Up @@ -159,6 +162,7 @@ export class Parser {
}
this.pos++;
this.ch++;
this.totalpos++;
}
},
skip: function () {
Expand All @@ -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() === "*";
Expand All @@ -193,6 +201,7 @@ export class Parser {
while (this.good() && this.current() !== "\n") {
this.pos++;
this.ch++;
this.totalpos++;
}
}
continue;
Expand All @@ -205,6 +214,7 @@ export class Parser {
lines.push(this.line);
} else this.ch++;
this.pos++;
this.totalpos++;
}
return lines;
},
Expand Down Expand Up @@ -251,6 +261,7 @@ export class Parser {
}
}
};
console.log(program);

// recursive compiler pass through the syntax tree
function compilerPass(passname) {
Expand Down
6 changes: 5 additions & 1 deletion src/lang/parser/parse_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lang/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit e313703

Please sign in to comment.