Various parser adjustments and compiler fixes
This commit is contained in:
parent
4fdd90614a
commit
8e53b19233
|
@ -381,16 +381,7 @@ proc patchJump(self: Compiler, offset: int) =
|
||||||
var jump: int = self.chunk.code.len() - offset
|
var jump: int = self.chunk.code.len() - offset
|
||||||
if jump > 16777215:
|
if jump > 16777215:
|
||||||
self.error("cannot jump more than 16777215 instructions")
|
self.error("cannot jump more than 16777215 instructions")
|
||||||
case OpCode(self.chunk.code[offset]):
|
let offsetArray = (jump - 4).toTriple()
|
||||||
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()
|
|
||||||
self.chunk.code[offset + 1] = offsetArray[0]
|
self.chunk.code[offset + 1] = offsetArray[0]
|
||||||
self.chunk.code[offset + 2] = offsetArray[1]
|
self.chunk.code[offset + 2] = offsetArray[1]
|
||||||
self.chunk.code[offset + 3] = offsetArray[2]
|
self.chunk.code[offset + 3] = offsetArray[2]
|
||||||
|
|
|
@ -826,7 +826,11 @@ proc ifStmt(self: Parser): Statement =
|
||||||
let thenBranch = self.blockStmt()
|
let thenBranch = self.blockStmt()
|
||||||
var elseBranch: Statement
|
var elseBranch: Statement
|
||||||
if self.match(Else):
|
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)
|
result = newIfStmt(condition, thenBranch, elseBranch, tok)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,5 +59,5 @@ proc fillSymbolTable*(tokenizer: Lexer) =
|
||||||
tokenizer.symbols.addKeyword("ref", TokenType.Ref)
|
tokenizer.symbols.addKeyword("ref", TokenType.Ref)
|
||||||
tokenizer.symbols.addKeyword("ptr", TokenType.Ptr)
|
tokenizer.symbols.addKeyword("ptr", TokenType.Ptr)
|
||||||
for sym in [">", "<", "=", "~", "/", "+", "-", "_", "*", "?", "@", ":", "==", "!=",
|
for sym in [">", "<", "=", "~", "/", "+", "-", "_", "*", "?", "@", ":", "==", "!=",
|
||||||
">=", "<=", "+=", "-=", "/=", "*=", "**=", "!"]:
|
">=", "<=", "+=", "-=", "/=", "*=", "**=", "!", "%"]:
|
||||||
tokenizer.symbols.addSymbol(sym, Symbol)
|
tokenizer.symbols.addSymbol(sym, Symbol)
|
42
tests/fib.pn
42
tests/fib.pn
|
@ -1,40 +1,4 @@
|
||||||
operator `<`(a, b: int): bool {
|
import std;
|
||||||
#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"]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn fib(n: int): int {
|
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();
|
var x = clock();
|
||||||
print(fib(30));
|
print(fib(37));
|
||||||
print(clock() - x);
|
print(clock() - x);
|
||||||
print("Done!");
|
print("Done!");
|
||||||
|
|
|
@ -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);
|
|
@ -13,4 +13,4 @@ print("END");
|
||||||
print(y);
|
print(y);
|
||||||
y = "test";
|
y = "test";
|
||||||
print(y);
|
print(y);
|
||||||
"";
|
"";
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
just a test
|
||||||
|
Starting GC torture test
|
||||||
|
1000000
|
||||||
|
END
|
||||||
|
just a test
|
||||||
|
test
|
|
@ -220,6 +220,10 @@ operator `**`*(a, b: uint64): uint64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
operator `%`*(a, b: int64): int64 {
|
||||||
|
#pragma[magic: "SignedMod", pure]
|
||||||
|
}
|
||||||
|
|
||||||
# Comparison operators
|
# Comparison operators
|
||||||
|
|
||||||
operator `>`*(a, b: int): bool {
|
operator `>`*(a, b: int): bool {
|
||||||
|
@ -533,7 +537,7 @@ operator `or`*(a, b: bool): bool {
|
||||||
|
|
||||||
# Assignment operators
|
# Assignment operators
|
||||||
|
|
||||||
operator `=`[T: Any](a: var T, b: T) {
|
operator `=`*[T: Any](a: var T, b: T) {
|
||||||
#pragma[magic: "GenericAssign"]
|
#pragma[magic: "GenericAssign"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue