From 98a78112677317d42d26f5dce8077ff5295abe8d Mon Sep 17 00:00:00 2001 From: Hudson Gouge Date: Sun, 5 May 2024 20:29:32 -0400 Subject: [PATCH] Strict mode now has very basic type checking. --- .../Errors/SyntaxHighlighter.adk | 3 +- Aardvark Compiler/Lexer.adk | 14 +++++----- Aardvark Compiler/Parser.adk | 2 +- Aardvark Interpreter/Exec.py | 28 +++++++++++++++++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Aardvark Compiler/Errors/SyntaxHighlighter.adk b/Aardvark Compiler/Errors/SyntaxHighlighter.adk index 23fccd3..2a43e4a 100644 --- a/Aardvark Compiler/Errors/SyntaxHighlighter.adk +++ b/Aardvark Compiler/Errors/SyntaxHighlighter.adk @@ -32,8 +32,9 @@ function Highlight(code, tokens, opts={}) { if token.position.start.index > last + 1 output += styles.default + code.slice(last+1, token.position.start.index) - if token.type == "String" + if token.type == "String" { output += styles.(token.type) + code.slice(token.position.start.index, token.position.end.index + 1) + } else if token.value == "\n" { line++ diff --git a/Aardvark Compiler/Lexer.adk b/Aardvark Compiler/Lexer.adk index 69e897d..abe8520 100644 --- a/Aardvark Compiler/Lexer.adk +++ b/Aardvark Compiler/Lexer.adk @@ -176,9 +176,9 @@ class Lexer as this { # Indents else if this.isWhitespace() & this.empty & this.useIndents { - value = '' - start = this.pos() - startcolumn = this.position.column + let value = '' + let start = this.pos() + let startcolumn = this.position.column while this.isWhitespace() & !this.AtEnd { value = value + this.current_character this.advance() @@ -300,8 +300,8 @@ class Lexer as this { # Multiline comments else if this.detect('#*') { - value = '' - start = this.pos() + let value = '' + let start = this.pos() while !this.detect('*#') & !this.AtEnd { value = value + this.current_character if this.current_character == '\n' this.newline() @@ -313,8 +313,8 @@ class Lexer as this { # Single line comments else if this.current_character == '#' { - value = '' - start = this.pos() + let value = '' + let start = this.pos() while this.current_character != '\n' & !this.AtEnd { value = value + this.current_character this.advance() diff --git a/Aardvark Compiler/Parser.adk b/Aardvark Compiler/Parser.adk index f3c79e9..a7a4a16 100644 --- a/Aardvark Compiler/Parser.adk +++ b/Aardvark Compiler/Parser.adk @@ -798,7 +798,7 @@ static class Parser as this { let is_embed_assignment = false if this.maybe_eat("Operator", "%") is_embed_assignment = true - assignments = [] + let assignments = [] while true { if assignments.length > 0 CHECK(this.eat("Delimiter", ",")) diff --git a/Aardvark Interpreter/Exec.py b/Aardvark Interpreter/Exec.py index fd5a413..d7dfa6a 100644 --- a/Aardvark Interpreter/Exec.py +++ b/Aardvark Interpreter/Exec.py @@ -303,6 +303,7 @@ def defineVar(self, name, value, scope, is_static=False, expr=None): name in list(scope.vars.keys()) and getattr(scope[name], "is_static", False) and type(scope[name]) != _Undefined + and expr ): start = expr["positions"]["start"] name_length = len(str(name)) @@ -320,9 +321,32 @@ def defineVar(self, name, value, scope, is_static=False, expr=None): }, ) else: + value = pyToAdk(value) + if ( + self.is_strict + and name in list(scope.vars.keys()) + and type(scope[name]) != _Undefined + and type(scope[name]) != type(value) + and expr + ): + start = expr["positions"]["start"] + name_length = len(str(name)) + self.errorhandler.throw( + "Type", + f"Cannot reassign a variable with a different type: {name}. Old type: {type(scope[name])}, New type: {type(value)}.", + { + "traceback": self.traceback, + "lineno": start["line"], + "marker": {"start": start["col"], "length": name_length}, + "underline": { + "start": start["col"] - 2, + "end": start["col"] + name_length, + }, + }, + ) if getattr(scope[name], "is_static", None) == True: is_static = True - scope[name] = pyToAdk(value) + scope[name] = value scope[name].is_static = is_static def makeFunct(self, expr, parent: Scope, is_macro=False): @@ -359,7 +383,7 @@ def x(*args, **kwargs): # notImplemented(self.errorhandler, "Type Checking", param) functscope.vars[param["name"]] = arg try: - if self.is_strict or param.get("is_static", False): + if param.get("is_static", False): setattr(functscope.vars[param["name"]], "is_static", True) # functscope.vars[param["name"]].is_static = True else: