Various parser adjustments and compiler fixes

This commit is contained in:
Mattia Giambirtone 2022-08-18 22:07:46 +02:00
parent 4fdd90614a
commit 8e53b19233
8 changed files with 47 additions and 53 deletions

View File

@ -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]

View File

@ -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)

View File

@ -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)

View File

@ -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!");

25
tests/fizzbuzz.pn Normal file
View File

@ -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);

View File

@ -13,4 +13,4 @@ print("END");
print(y);
y = "test";
print(y);
"";
"";

6
tests/log Normal file
View File

@ -0,0 +1,6 @@
just a test
Starting GC torture test
1000000
END
just a test
test

View File

@ -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"]
}