Skip to content

Commit 98a7811

Browse files
committed
Strict mode now has very basic type checking.
1 parent bd1d970 commit 98a7811

4 files changed

Lines changed: 36 additions & 11 deletions

File tree

Aardvark Compiler/Errors/SyntaxHighlighter.adk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ function Highlight(code, tokens, opts={}) {
3232
if token.position.start.index > last + 1
3333
output += styles.default + code.slice(last+1, token.position.start.index)
3434

35-
if token.type == "String"
35+
if token.type == "String" {
3636
output += styles.(token.type) + code.slice(token.position.start.index, token.position.end.index + 1)
37+
}
3738

3839
else if token.value == "\n" {
3940
line++

Aardvark Compiler/Lexer.adk

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ class Lexer as this {
176176

177177
# Indents
178178
else if this.isWhitespace() & this.empty & this.useIndents {
179-
value = ''
180-
start = this.pos()
181-
startcolumn = this.position.column
179+
let value = ''
180+
let start = this.pos()
181+
let startcolumn = this.position.column
182182
while this.isWhitespace() & !this.AtEnd {
183183
value = value + this.current_character
184184
this.advance()
@@ -300,8 +300,8 @@ class Lexer as this {
300300

301301
# Multiline comments
302302
else if this.detect('#*') {
303-
value = ''
304-
start = this.pos()
303+
let value = ''
304+
let start = this.pos()
305305
while !this.detect('*#') & !this.AtEnd {
306306
value = value + this.current_character
307307
if this.current_character == '\n' this.newline()
@@ -313,8 +313,8 @@ class Lexer as this {
313313

314314
# Single line comments
315315
else if this.current_character == '#' {
316-
value = ''
317-
start = this.pos()
316+
let value = ''
317+
let start = this.pos()
318318
while this.current_character != '\n' & !this.AtEnd {
319319
value = value + this.current_character
320320
this.advance()

Aardvark Compiler/Parser.adk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ static class Parser as this {
798798
let is_embed_assignment = false
799799
if this.maybe_eat("Operator", "%")
800800
is_embed_assignment = true
801-
assignments = []
801+
let assignments = []
802802
while true {
803803
if assignments.length > 0
804804
CHECK(this.eat("Delimiter", ","))

Aardvark Interpreter/Exec.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def defineVar(self, name, value, scope, is_static=False, expr=None):
303303
name in list(scope.vars.keys())
304304
and getattr(scope[name], "is_static", False)
305305
and type(scope[name]) != _Undefined
306+
and expr
306307
):
307308
start = expr["positions"]["start"]
308309
name_length = len(str(name))
@@ -320,9 +321,32 @@ def defineVar(self, name, value, scope, is_static=False, expr=None):
320321
},
321322
)
322323
else:
324+
value = pyToAdk(value)
325+
if (
326+
self.is_strict
327+
and name in list(scope.vars.keys())
328+
and type(scope[name]) != _Undefined
329+
and type(scope[name]) != type(value)
330+
and expr
331+
):
332+
start = expr["positions"]["start"]
333+
name_length = len(str(name))
334+
self.errorhandler.throw(
335+
"Type",
336+
f"Cannot reassign a variable with a different type: {name}. Old type: {type(scope[name])}, New type: {type(value)}.",
337+
{
338+
"traceback": self.traceback,
339+
"lineno": start["line"],
340+
"marker": {"start": start["col"], "length": name_length},
341+
"underline": {
342+
"start": start["col"] - 2,
343+
"end": start["col"] + name_length,
344+
},
345+
},
346+
)
323347
if getattr(scope[name], "is_static", None) == True:
324348
is_static = True
325-
scope[name] = pyToAdk(value)
349+
scope[name] = value
326350
scope[name].is_static = is_static
327351

328352
def makeFunct(self, expr, parent: Scope, is_macro=False):
@@ -359,7 +383,7 @@ def x(*args, **kwargs):
359383
# notImplemented(self.errorhandler, "Type Checking", param)
360384
functscope.vars[param["name"]] = arg
361385
try:
362-
if self.is_strict or param.get("is_static", False):
386+
if param.get("is_static", False):
363387
setattr(functscope.vars[param["name"]], "is_static", True)
364388
# functscope.vars[param["name"]].is_static = True
365389
else:

0 commit comments

Comments
 (0)