Implement Control Flow Statements in Kym #37

Merged
kayomn merged 8 commits from kym-control-flow into main 2023-08-26 00:01:25 +02:00
1 changed files with 19 additions and 9 deletions
Showing only changes of commit eca2cfffb8 - Show all commits

View File

@ -272,16 +272,15 @@ pub const RuntimeEnv = struct {
try self.compile_expression(chunk, @"if".condition_expression);
try chunk.opcodes.push_one(.{.jf = 0});
const jump_opcode = &chunk.opcodes.values[chunk.opcodes.values.len - 1];
const jump_opcode_index = chunk.opcodes.values.len - 1;
for (@"if".block_statements.values) |block_statement| {
try self.compile_statement(chunk, block_statement);
}
coral.debug.assert(jump_opcode.* == .jf);
coral.debug.assert(chunk.opcodes.values.len < coral.math.max_int(@typeInfo(u32).Int));
jump_opcode.jf = @intCast(chunk.opcodes.values.len);
chunk.opcodes.values[jump_opcode_index].jf = @intCast(chunk.opcodes.values.len - 1);
},
.expression => |expression| try self.compile_expression(chunk, expression),
@ -422,15 +421,11 @@ pub const RuntimeEnv = struct {
},
.object_get => {
const index = (try self.pop_local()) orelse {
return self.env.raise(error.TypeMismatch, "nil is not a valid index");
};
const index = try self.env.expect(try self.pop_local());
defer self.env.discard(index);
const indexable = (try self.pop_local()) orelse {
return self.env.raise(error.TypeMismatch, "nil is not a valid indexable");
};
const indexable = try self.env.expect(try self.pop_local());
defer self.env.discard(indexable);
@ -1554,6 +1549,21 @@ pub const RuntimeRef = opaque {
.dynamic => true,
};
}
pub fn typename(self: *const RuntimeRef) []const coral.io.Byte {
return switch (self.object().payload) {
.false => "false",
.true => "true",
.float => "float",
.fixed => "fixed",
.symbol => "symbol",
.vector2 => "vector2",
.vector3 => "vector3",
.syscall => "syscall",
.string => "string",
.dynamic => "dynamic",
};
}
};
pub const Syscall = fn (env: *RuntimeEnv) RuntimeError!?*RuntimeRef;