Compare commits

..

3 Commits

Author SHA1 Message Date
kayomn 77af4d067b Fix > and == operators being miscompiled
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2023-08-25 22:57:33 +01:00
kayomn c4916a4195 Clean up statement AST generation logic 2023-08-25 22:45:48 +01:00
kayomn 08a63489b0 Add if / else 2023-08-24 23:45:29 +01:00
4 changed files with 210 additions and 128 deletions

View File

@ -1,12 +1,20 @@
i = 0
while i < 5 do
while i < 5:
@print("hello, world")
i = i + 1
end
if i > 6:
@print("`i` greater than `6`")
elif i == 4:
@print("`i` is equal to `4`")
else:
@print("i'unno")
end
return {
.title = "Game",
.width = 1280,

View File

@ -36,6 +36,7 @@ pub const RuntimeEnv = struct {
};
const Opcode = union (enum) {
pop,
push_nil,
push_true,
push_false,
@ -135,9 +136,9 @@ pub const RuntimeEnv = struct {
.subtraction => .sub,
.multiplication => .mul,
.divsion => .div,
.greater_equals_comparison => .eql,
.greater_equals_comparison => .cge,
.greater_than_comparison => .cgt,
.equals_comparison => .cge,
.equals_comparison => .eql,
.less_than_comparison => .clt,
.less_equals_comparison => .cle,
});
@ -263,13 +264,19 @@ pub const RuntimeEnv = struct {
fn compile_statement(self: *CompilationUnit, chunk: *Chunk, statement: ast.Statement) RuntimeError!void {
switch (statement) {
.@"return" => |@"return"| {
if (@"return".expression) |expression| {
if (@"return") |expression| {
try self.compile_expression(chunk, expression);
} else {
try chunk.opcodes.push_one(.push_nil);
}
},
.block => |block| {
for (block.values) |block_statement| {
try self.compile_statement(chunk, block_statement);
}
},
.@"while" => |@"while"| {
try self.compile_expression(chunk, @"while".condition_expression);
try chunk.opcodes.push_one(.{.jf = 0});
@ -280,8 +287,6 @@ pub const RuntimeEnv = struct {
try self.compile_statement(chunk, block_statement);
}
coral.debug.assert(chunk.opcodes.values.len < coral.math.max_int(@typeInfo(u32).Int));
chunk.opcodes.values[origin_index].jf = @intCast(chunk.opcodes.values.len - 1);
try self.compile_expression(chunk, @"while".condition_expression);
@ -298,12 +303,20 @@ pub const RuntimeEnv = struct {
try self.compile_statement(chunk, block_statement);
}
coral.debug.assert(chunk.opcodes.values.len < coral.math.max_int(@typeInfo(u32).Int));
chunk.opcodes.values[origin_index].jf = @intCast(chunk.opcodes.values.len - 1);
if (@"if".else_statement) |else_statement| {
try self.compile_statement(chunk, else_statement.*);
}
},
.expression => |expression| try self.compile_expression(chunk, expression),
.expression => |expression| {
try self.compile_expression(chunk, expression);
if (expression == .invoke) {
try chunk.opcodes.push_one(.pop);
}
},
}
}
@ -326,10 +339,10 @@ pub const RuntimeEnv = struct {
}
};
fn compile(self: *Chunk, statements: *const ast.StatementList) RuntimeError!void {
fn compile(self: *Chunk, statements: []const ast.Statement) RuntimeError!void {
var unit = CompilationUnit{};
for (statements.values) |statement| {
for (statements) |statement| {
try unit.compile_statement(self, statement);
}
}
@ -364,6 +377,12 @@ pub const RuntimeEnv = struct {
while (opcode_cursor < self.opcodes.values.len) : (opcode_cursor += 1) {
switch (self.opcodes.values[opcode_cursor]) {
.pop => {
if (try self.pop_local()) |ref| {
self.env.discard(ref);
}
},
.push_nil => try self.env.locals.push_one(null),
.push_true => try self.env.locals.push_one(try self.env.new_boolean(true)),
.push_false => try self.env.locals.push_one(try self.env.new_boolean(false)),

View File

@ -118,9 +118,11 @@ pub const Expression = union (enum) {
invoke: struct {
object_expression: *Expression,
argument_expressions: ExpressionList,
argument_expressions: List,
},
const List = coral.list.Stack(Expression);
const TableLiteral = coral.list.Stack(struct {
key_expression: Expression,
value_expression: Expression,
@ -129,37 +131,38 @@ pub const Expression = union (enum) {
const ExpressionBuilder = fn (self: *Tree) ParseError!Expression;
pub const ExpressionList = coral.list.Stack(Expression);
pub const ParseError = error {
OutOfMemory,
BadSyntax,
};
pub const Statement = union (enum) {
@"return": struct {
expression: ?Expression,
@"return": ?Expression,
@"if": struct {
condition_expression: Expression,
block_statements: List,
else_statement: ?*Statement,
},
@"if": ConditionalBlock,
@"while": ConditionalBlock,
@"while": struct {
condition_expression: Expression,
block_statements: List,
},
block: List,
expression: Expression,
const ConditionalBlock = struct {
condition_expression: Expression,
block_statements: StatementList,
};
const List = coral.list.Stack(Statement);
};
pub const StatementList = coral.list.Stack(Statement);
pub const Tree = struct {
name: []const coral.io.Byte,
allocator: coral.io.Allocator,
arena: coral.arena.Stacking,
error_buffer: coral.list.ByteStack,
tokenizer: tokens.Tokenizer,
parsed_statements: StatementList,
parsed_statements: Statement.List,
has_returned: bool,
pub fn error_message(self: Tree) []const coral.io.Byte {
@ -176,7 +179,7 @@ pub const Tree = struct {
return .{
.arena = coral.arena.Stacking.make(allocator, 4096),
.error_buffer = coral.list.ByteStack.make(allocator),
.parsed_statements = StatementList.make(allocator),
.parsed_statements = Statement.List.make(allocator),
.tokenizer = .{.source = ""},
.allocator = allocator,
.name = ast_name,
@ -184,19 +187,19 @@ pub const Tree = struct {
};
}
pub fn parse(self: *Tree, data: []const coral.io.Byte) ParseError!*const StatementList {
pub fn parse(self: *Tree, data: []const coral.io.Byte) ParseError![]const Statement {
self.free();
self.tokenizer = .{.source = data};
self.has_returned = false;
self.parsed_statements.free();
self.tokenizer.skip_newlines();
self.parsed_statements = try self.parse_statements();
if (self.tokenizer.token == .keyword_end) {
return self.report("unexpected `end` without matching `do` block");
while (self.tokenizer.token != .end) {
try self.parsed_statements.push_one(try self.parse_statement());
}
return &self.parsed_statements;
return self.parsed_statements.values;
}
const parse_additive = BinaryOperator.builder(parse_equality, &.{
@ -204,6 +207,74 @@ pub const Tree = struct {
.subtraction,
});
fn parse_branch(self: *Tree) ParseError!Statement {
const allocator = self.arena.as_allocator();
defer self.tokenizer.skip_newlines();
self.tokenizer.step();
const condition_expression = try self.parse_expression();
if (self.tokenizer.token != .symbol_colon) {
return self.report("expected `:` after `if` statement condition");
}
var statements = Statement.List.make(allocator);
self.tokenizer.skip_newlines();
while (true) {
switch (self.tokenizer.token) {
.keyword_end => {
return .{
.@"if" = .{
.condition_expression = condition_expression,
.block_statements = statements,
.else_statement = null,
},
};
},
.keyword_else => {
self.tokenizer.step();
if (self.tokenizer.token != .symbol_colon) {
return self.report("expected `:` after `if` statement condition");
}
var else_statements = Statement.List.make(allocator);
self.tokenizer.skip_newlines();
while (self.tokenizer.token != .keyword_end) {
try else_statements.push_one(try self.parse_statement());
}
return .{
.@"if" = .{
.else_statement = try coral.io.allocate_one(allocator, Statement{.block = else_statements}),
.condition_expression = condition_expression,
.block_statements = statements,
},
};
},
.keyword_elif => {
return .{
.@"if" = .{
.else_statement = try coral.io.allocate_one(allocator, try self.parse_branch()),
.condition_expression = condition_expression,
.block_statements = statements,
},
};
},
else => try statements.push_one(try self.parse_statement()),
}
}
}
const parse_comparison = BinaryOperator.builder(parse_term, &.{
.greater_than_comparison,
.greater_equals_comparison,
@ -486,7 +557,7 @@ pub const Tree = struct {
},
.symbol_paren_left => {
var argument_expressions = ExpressionList.make(allocator);
var argument_expressions = Expression.List.make(allocator);
while (true) {
self.tokenizer.skip_newlines();
@ -526,25 +597,21 @@ pub const Tree = struct {
return expression;
}
fn parse_statements(self: *Tree) ParseError!StatementList {
var statements = StatementList.make(self.arena.as_allocator());
fn parse_statement(self: *Tree) ParseError!Statement {
const allocator = self.arena.as_allocator();
self.tokenizer.skip_newlines();
while (true) {
try statements.push_one(parse_statement: {
switch (self.tokenizer.token) {
.end, .keyword_end => return statements,
.keyword_return => {
defer self.tokenizer.skip_newlines();
if (self.has_returned) {
return self.report("multiple returns in function scope but expected only one");
return self.report("multiple returns in lambda scope but expected only one");
}
self.tokenizer.step();
if (self.tokenizer.token != .end and self.tokenizer.token != .newline) {
break: parse_statement .{.@"return" = .{.expression = try self.parse_expression()}};
return .{.@"return" = try self.parse_expression()};
}
if (self.tokenizer.token != .end and self.tokenizer.token != .newline) {
@ -553,66 +620,38 @@ pub const Tree = struct {
self.has_returned = true;
break: parse_statement .{.@"return" = .{.expression = null}};
return .{.@"return" = null};
},
.keyword_while => {
defer self.tokenizer.skip_newlines();
self.tokenizer.step();
const condition_expression = try self.parse_expression();
if (self.tokenizer.token != .keyword_do) {
return self.report("expected `do` block after `while` statement");
if (self.tokenizer.token != .symbol_colon) {
return self.report("expected `:` after `while` statement");
}
self.tokenizer.step();
var statements = Statement.List.make(allocator);
const while_statement = Statement{
self.tokenizer.skip_newlines();
while (self.tokenizer.token != .keyword_end) {
try statements.push_one(try self.parse_statement());
}
return .{
.@"while" = .{
.block_statements = try self.parse_statements(),
.block_statements = statements,
.condition_expression = condition_expression,
},
};
if (self.tokenizer.token != .keyword_end) {
return self.report("expected `end` after block");
}
self.tokenizer.skip_newlines();
break: parse_statement while_statement;
},
.keyword_if => {
self.tokenizer.step();
const condition_expression = try self.parse_expression();
if (self.tokenizer.token != .keyword_do) {
return self.report("expected `do` block after `if` statement");
}
self.tokenizer.step();
const if_statement = Statement{
.@"if" = .{
.block_statements = try self.parse_statements(),
.condition_expression = condition_expression,
},
};
if (self.tokenizer.token != .keyword_end) {
return self.report("expected `end` after block");
}
self.tokenizer.skip_newlines();
break: parse_statement if_statement;
},
else => break: parse_statement .{.expression = try self.parse_expression()},
}
});
.keyword_if => return self.parse_branch(),
else => return .{.expression = try self.parse_expression()},
}
}

View File

@ -21,7 +21,7 @@ pub const Token = union(enum) {
symbol_bracket_left,
symbol_bracket_right,
symbol_period,
symbol_lambda,
symbol_colon,
symbol_less_than,
symbol_less_equals,
symbol_greater_than,
@ -42,6 +42,8 @@ pub const Token = union(enum) {
keyword_do,
keyword_end,
keyword_while,
keyword_else,
keyword_elif,
pub fn text(self: Token) []const coral.io.Byte {
return switch (self) {
@ -66,7 +68,7 @@ pub const Token = union(enum) {
.symbol_bracket_left => "[",
.symbol_bracket_right => "]",
.symbol_period => ".",
.symbol_lambda => "=>",
.symbol_colon => ":",
.symbol_less_than => "<",
.symbol_less_equals => "<=",
.symbol_greater_than => ">",
@ -87,6 +89,8 @@ pub const Token = union(enum) {
.keyword_do => "do",
.keyword_end => "end",
.keyword_while => "while",
.keyword_elif => "elif",
.keyword_else => "else",
};
}
};
@ -94,7 +98,7 @@ pub const Token = union(enum) {
pub const Tokenizer = struct {
source: []const coral.io.Byte,
lines_stepped: usize = 1,
token: Token = .end,
token: Token = .newline,
pub fn skip_newlines(self: *Tokenizer) void {
self.step();
@ -190,6 +194,18 @@ pub const Tokenizer = struct {
},
'e' => {
if (coral.io.ends_with(identifier, "lse")) {
self.token = .keyword_else;
return;
}
if (coral.io.ends_with(identifier, "lif")) {
self.token = .keyword_elif;
return;
}
if (coral.io.ends_with(identifier, "nd")) {
self.token = .keyword_end;
@ -378,6 +394,13 @@ pub const Tokenizer = struct {
return;
},
':' => {
self.token = .symbol_colon;
cursor += 1;
return;
},
'=' => {
cursor += 1;
@ -390,13 +413,6 @@ pub const Tokenizer = struct {
return;
},
'>' => {
cursor += 1;
self.token = .symbol_lambda;
return;
},
else => {},
}
}