Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Nov 12, 2024
1 parent e3de8dd commit 3b4c500
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/ast/nodes/BinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export enum Operator {
Equals = "=",
EqualsEquals = "==",
EqualsEqualsEquals = "===",
LessThanEquals = "<="
}
11 changes: 10 additions & 1 deletion src/ast/nodes/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@ import { Range } from "../Range";

export class Expression {
public nameOf: string = "Expression";
public range: Range = new Range(-1, -1, -1);
public range: Range = new Range(
{
line: -1,
column: -1
},
{
line: -1,
column: -1
}
);
}
25 changes: 22 additions & 3 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ export class Parser {
parseExpression(scope: Scope, besides: string | null = null): Expression | null {
let express: Expression | null = null;
const state = this.source.tokenizer.createState();
if (besides !== "CallExpression" && (express = this.parseCallExpression(scope))) return express;
state.resume();
if (besides !== "BinaryExpression" && (express = this.parseBinaryExpression(scope))) return express;
state.resume();
if (besides !== "CallExpression" && (express = this.parseCallExpression(scope))) return express;
state.resume();
if (besides !== "NumberLiteral" && (express = this.parseNumberLiteral(scope))) return express;
state.resume();
if (besides !== "StringLiteral" && (express = this.parseStringLiteral(scope))) return express;
Expand Down Expand Up @@ -557,6 +557,7 @@ export class Parser {
}
parseCallExpression(scope: Scope): CallExpression | null {
const calling = this.source.tokenizer.getToken();

Check failure on line 559 in src/parser/index.ts

View workflow job for this annotation

GitHub Actions / build

TypeError: undefined is not an object (evaluating 'this.source.tokenizer.getToken')

at parseCallExpression (/home/runner/work/Zep/Zep/src/parser/index.ts:559:33) at /home/runner/work/Zep/Zep/tests/parser.test.ts:82:25
if (!isIdentifier(calling)) return null;
const leftParen = this.source.tokenizer.getToken();
if (leftParen.text !== "(") return null;
const args: Expression[] = [];
Expand Down Expand Up @@ -636,8 +637,25 @@ export class Parser {
);
return node;
}
parseBinaryExpressionLeft(scope: Scope) {
const state = this.source.tokenizer.createState();
let left: Expression | null;
if (left = this.parseCallExpression(scope)) return left;
state.resume();
if (left = this.parseNumberLiteral(scope)) return left;
state.resume();
if (left = this.parseIdentifierExpression(scope)) return left;
state.resume();
if (left = this.parseStringLiteral(scope)) return left;
state.resume();
if (left = this.parseBooleanLiteral(scope)) return left;
state.resume();
if (left = this.parseParenthesizedExpression(scope)) return left;
state.resume()
return null;
}
parseBinaryExpression(scope: Scope): BinaryExpression | null {
let left: Expression | null = this.parseIdentifierExpression(scope) || this.parseNumberLiteral(scope) || this.parseIdentifierExpression(scope) || this.parseStringLiteral(scope) || this.parseBooleanLiteral(scope) || this.parseParenthesizedExpression(scope);
let left: Expression | null = this.parseBinaryExpressionLeft(scope);
if (!left) return null;
const op = tokenToOp(this.source.tokenizer.getToken());
let right: Expression | null = this.parseExpression(scope);
Expand Down Expand Up @@ -779,5 +797,6 @@ export function tokenToOp(tok: TokenData): Operator | null {
if (tok.token === Token.Equals) return Operator.Equals;
if (tok.token === Token.EqualsEquals) return Operator.EqualsEquals;
if (tok.token === Token.EqualsEqualsEquals) return Operator.EqualsEqualsEquals;
if (tok.token === Token.LessThanEquals) return Operator.LessThanEquals;
return null;
}
23 changes: 12 additions & 11 deletions src/scripts/fib.zp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#[export]
import "std:io"

fn fib(n: i32) -> i32 {
i32 a = 0
i32 b = 1
if (n > 0) {
while (--n) {
i32 t = a + b
a = b
b = t
if n <= 1 {
rt n
} else {
rt fib(n - 1) + fib(n - 2)
}
return b
}
return a
}

#[export]
fn main() -> void {
i32 result = fib(7)
print(result)
}
29 changes: 12 additions & 17 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { Generator } from "./generator";
import { Scope } from "./checker/scope/Scope";
import { Source, SourceKind } from "./ast/Source";
import { Program } from "./ast/Program";
import { SyntaxColors } from "./formatter/syntaxcolors";

const start = Date.now();
const program = new Program([
new Source(
"std:io/print",
"std:io",
`
#[extern]: env
#[export]
Expand All @@ -21,21 +22,21 @@ const program = new Program([
new Source(
"test.zp",
`
import "std:io/print"
import "std:io"
fn factorial(n: i32) -> i32 {
if n == 0 {
rt 1
fn fib(n: i32) -> i32 {
if n <= 1 {
rt n
} else {
rt n * factorial(n - 1)
rt fib(n - 1) + fib(n - 2)
}
}
}
#[export]
fn main() -> void {
i32 result = factorial(5)
#[export]
fn main() -> void {
i32 result = fib(16)
print(result)
}
}
`,
SourceKind.UserEntry
)
Expand All @@ -48,9 +49,3 @@ console.dir(source.topLevelStatements, { depth: 1 });
// console.log(new Program([new Source("test.zp", 'import "std:io/print"', SourceKind.UserEntry)]).entry.parser.parseImportDeclaration(new Scope()))
const transpiled = Formatter.from(source);
console.log("\n" + transpiled.trim());

// const generator = new Generator();
// generator.parseFn(program.topLevelStatements[1])
// console.log("WAT:\n" + generator.toWat());

// writeFileSync("./test.ts", transpiled);
1 change: 1 addition & 0 deletions src/tokenizer/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum Token {
// COMPARISIONS
GreaterThan, // >
LessThan, // <
LessThanEquals, // <=
// SYMBOLS
Pound, // #
// UTILITY
Expand Down
9 changes: 9 additions & 0 deletions src/tokenizer/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export function isPunctuation(
} else if (text.startsWith("=", position.current)) {
position.current++;
return new TokenData(Token.Equals, "=", position.toRange());
} else if (text.startsWith(":=", position.current)) {
position.current += 2;
return new TokenData(Token.ColonEquals, ":=", position.toRange());
} else if (text.startsWith("?=", position.current)) {
position.current += 2;
return new TokenData(Token.QuestionEquals, "?=", position.toRange());
} else if (text.startsWith("<=", position.current)) {
position.current += 2;
return new TokenData(Token.LessThanEquals, "<=", position.toRange());
} else if (text.startsWith("?", position.current)) {
position.current++;
return new TokenData(Token.Question, "?", position.toRange());
Expand Down

0 comments on commit 3b4c500

Please sign in to comment.