Skip to content

Commit

Permalink
Strict mode now has very basic type checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
hg0428 committed May 6, 2024
1 parent bd1d970 commit 98a7811
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Aardvark Compiler/Errors/SyntaxHighlighter.adk
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
14 changes: 7 additions & 7 deletions Aardvark Compiler/Lexer.adk
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Aardvark Compiler/Parser.adk
Original file line number Diff line number Diff line change
Expand Up @@ -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", ","))
Expand Down
28 changes: 26 additions & 2 deletions Aardvark Interpreter/Exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 98a7811

Please sign in to comment.