Implement Control Flow Statements in Kym #37
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue