Compare commits
No commits in common. "0b60b327ce2a6d5ccad006086160cf70efac1cd1" and "4fdd90614a5a3912b365b9dfbbc8a3809d0393dd" have entirely different histories.
0b60b327ce
...
4fdd90614a
|
@ -381,7 +381,16 @@ 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")
|
||||||
let offsetArray = (jump - 4).toTriple()
|
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()
|
||||||
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,10 +826,6 @@ 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):
|
||||||
if self.match(If):
|
|
||||||
elseBranch = self.ifStmt()
|
|
||||||
else:
|
|
||||||
self.expect(LeftBrace, "expecting 'if' or block statement")
|
|
||||||
elseBranch = self.blockStmt()
|
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,4 +1,40 @@
|
||||||
import std;
|
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"]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn fib(n: int): int {
|
fn fib(n: int): int {
|
||||||
|
@ -9,8 +45,8 @@ fn fib(n: int): int {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
print("Computing the value of fib(37)");
|
print("Computing the value of fib(30)");
|
||||||
var x = clock();
|
var x = clock();
|
||||||
print(fib(37));
|
print(fib(30));
|
||||||
print(clock() - x);
|
print(clock() - x);
|
||||||
print("Done!");
|
print("Done!");
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
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);
|
|
|
@ -220,10 +220,6 @@ 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 {
|
||||||
|
@ -537,7 +533,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