diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index fa3ad75..e737726 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -381,16 +381,7 @@ proc patchJump(self: Compiler, offset: int) = var jump: int = self.chunk.code.len() - offset if jump > 16777215: self.error("cannot jump more than 16777215 instructions") - case OpCode(self.chunk.code[offset]): - of JumpBackwards, Jump, JumpIfFalsePop, JumpIfFalse: - # We subtract 4 because backwards - # and absolute jumps don't take - # the size of the jump offset - # into account - jump -= 4 - else: - discard - let offsetArray = jump.toTriple() + let offsetArray = (jump - 4).toTriple() self.chunk.code[offset + 1] = offsetArray[0] self.chunk.code[offset + 2] = offsetArray[1] self.chunk.code[offset + 3] = offsetArray[2] diff --git a/src/frontend/parser.nim b/src/frontend/parser.nim index 149bc91..b311c06 100644 --- a/src/frontend/parser.nim +++ b/src/frontend/parser.nim @@ -826,7 +826,11 @@ proc ifStmt(self: Parser): Statement = let thenBranch = self.blockStmt() var elseBranch: Statement if self.match(Else): - elseBranch = self.blockStmt() + if self.match(If): + elseBranch = self.ifStmt() + else: + self.expect(LeftBrace, "expecting 'if' or block statement") + elseBranch = self.blockStmt() result = newIfStmt(condition, thenBranch, elseBranch, tok) diff --git a/src/util/symbols.nim b/src/util/symbols.nim index c7f2dca..698f164 100644 --- a/src/util/symbols.nim +++ b/src/util/symbols.nim @@ -59,5 +59,5 @@ proc fillSymbolTable*(tokenizer: Lexer) = tokenizer.symbols.addKeyword("ref", TokenType.Ref) tokenizer.symbols.addKeyword("ptr", TokenType.Ptr) for sym in [">", "<", "=", "~", "/", "+", "-", "_", "*", "?", "@", ":", "==", "!=", - ">=", "<=", "+=", "-=", "/=", "*=", "**=", "!"]: + ">=", "<=", "+=", "-=", "/=", "*=", "**=", "!", "%"]: tokenizer.symbols.addSymbol(sym, Symbol) \ No newline at end of file diff --git a/tests/fib.pn b/tests/fib.pn index 14be2ea..d562c0e 100644 --- a/tests/fib.pn +++ b/tests/fib.pn @@ -1,40 +1,4 @@ -operator `<`(a, b: int): bool { - #pragma[magic: "LessThan", pure] -} - - -operator `+`(a, b: int): int { - #pragma[magic: "Add", pure] -} - - -operator `-`(a, b: int): int { - #pragma[magic: "Subtract", pure] -} - - -operator `-`(a, b: float): float { - #pragma[magic: "SubtractFloat64", pure] -} - -fn clock: float { - #pragma[magic: "SysClock64"] -} - - -fn print(x: int) { - #pragma[magic: "PrintInt64"] -} - - -fn print(x: float) { - #pragma[magic: "PrintFloat64"] -} - - -fn print(x: string) { - #pragma[magic: "PrintString"] -} +import std; fn fib(n: int): int { @@ -45,8 +9,8 @@ fn fib(n: int): int { } -print("Computing the value of fib(30)"); +print("Computing the value of fib(37)"); var x = clock(); -print(fib(30)); +print(fib(37)); print(clock() - x); print("Done!"); diff --git a/tests/fizzbuzz.pn b/tests/fizzbuzz.pn new file mode 100644 index 0000000..78c0ee7 --- /dev/null +++ b/tests/fizzbuzz.pn @@ -0,0 +1,25 @@ +import std; + + +fn fizzBuzz(n: int) { + var x = 0; + while x < n { + x = x + 1; + if x % 15 == 0 { + print("FizzBuzz"); + } + if x % 3 == 0 { + print("Fizz"); + } + else if x % 5 == 0 { + print("Buzz"); + } + else { + print(x); + } + } +} + + + +fizzBuzz(30); \ No newline at end of file diff --git a/tests/gc.pn b/tests/gc.pn index 0d49abe..b5b9b5a 100644 --- a/tests/gc.pn +++ b/tests/gc.pn @@ -13,4 +13,4 @@ print("END"); print(y); y = "test"; print(y); -""; \ No newline at end of file +""; diff --git a/tests/log b/tests/log new file mode 100644 index 0000000..753f1e9 --- /dev/null +++ b/tests/log @@ -0,0 +1,6 @@ +just a test +Starting GC torture test +1000000 +END +just a test +test diff --git a/tests/std.pn b/tests/std.pn index 2216cc2..c7aebd4 100644 --- a/tests/std.pn +++ b/tests/std.pn @@ -220,6 +220,10 @@ operator `**`*(a, b: uint64): uint64 { } +operator `%`*(a, b: int64): int64 { + #pragma[magic: "SignedMod", pure] +} + # Comparison operators operator `>`*(a, b: int): bool { @@ -533,7 +537,7 @@ operator `or`*(a, b: bool): bool { # Assignment operators -operator `=`[T: Any](a: var T, b: T) { +operator `=`*[T: Any](a: var T, b: T) { #pragma[magic: "GenericAssign"] }