Compare commits
No commits in common. "c30f10db96af773bd13a7d793a347a623085ddea" and "b28b1a86c96ba3cd920173c6a55cdbf20fbf265e" have entirely different histories.
c30f10db96
...
b28b1a86c9
|
@ -1,6 +1,9 @@
|
|||
|
||||
let tool = "wrench"
|
||||
var test_param = `monkey {tool} {2 + 1 - 1}`
|
||||
let wrench = "wrench"
|
||||
|
||||
var test_param = "monkey {wrench}"
|
||||
|
||||
test_param = "monkey"
|
||||
|
||||
let printer = lambda (pfx):
|
||||
@print(test_param)
|
||||
|
|
|
@ -17,7 +17,7 @@ pub fn Stack(comptime Value: type) type {
|
|||
}
|
||||
|
||||
pub fn drop(self: *Self, count: usize) bool {
|
||||
if (math.checked_sub(self.values.len, count)) |updated_count| {
|
||||
if (math.checked_sub(self.values, count)) |updated_count| {
|
||||
self.values = self.values[0 .. updated_count];
|
||||
|
||||
return true;
|
||||
|
|
|
@ -133,80 +133,6 @@ pub const RuntimeEnv = struct {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn concat(self: *RuntimeEnv, values: []const *const RuntimeRef) RuntimeError!*RuntimeRef {
|
||||
if (values.len == 0) {
|
||||
return self.new_string("");
|
||||
}
|
||||
|
||||
var remaining_value_count = values.len;
|
||||
|
||||
errdefer {
|
||||
var poppable_value_count = values.len - remaining_value_count;
|
||||
|
||||
while (poppable_value_count != 0) {
|
||||
poppable_value_count -= 1;
|
||||
|
||||
const popped_value = self.pop_local() catch unreachable;
|
||||
|
||||
coral.debug.assert(popped_value != null);
|
||||
self.discard(popped_value.?);
|
||||
}
|
||||
}
|
||||
|
||||
var length = @as(usize, 0);
|
||||
|
||||
while (remaining_value_count != 0) {
|
||||
remaining_value_count -= 1;
|
||||
|
||||
const value_string = try self.to_string(values[remaining_value_count]);
|
||||
|
||||
errdefer self.discard(value_string);
|
||||
|
||||
const string = value_string.as_string();
|
||||
|
||||
coral.debug.assert(string != null);
|
||||
|
||||
try self.locals.push_one(value_string);
|
||||
|
||||
length += string.?.len;
|
||||
}
|
||||
|
||||
const buffer = try coral.io.allocate_many(self.allocator, length, @as(coral.io.Byte, 0));
|
||||
|
||||
errdefer self.allocator.deallocate(buffer);
|
||||
|
||||
var written = @as(usize, 0);
|
||||
|
||||
while (remaining_value_count != values.len) : (remaining_value_count += 1) {
|
||||
const value_string = try self.pop_local();
|
||||
|
||||
coral.debug.assert(value_string != null);
|
||||
|
||||
defer self.discard(value_string.?);
|
||||
|
||||
const string = value_string.?.as_string();
|
||||
|
||||
coral.debug.assert(string != null);
|
||||
|
||||
coral.io.copy(buffer[written ..], string.?);
|
||||
|
||||
written += string.?.len;
|
||||
|
||||
coral.debug.assert(written <= length);
|
||||
}
|
||||
|
||||
return RuntimeRef.allocate(self.allocator, .{
|
||||
.ref_count = 1,
|
||||
|
||||
.payload = .{
|
||||
.string = .{
|
||||
.ptr = buffer.ptr,
|
||||
.len = @intCast(buffer.len),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
pub fn discard(self: *RuntimeEnv, value: *RuntimeRef) void {
|
||||
var object = value.object();
|
||||
|
||||
|
@ -647,9 +573,8 @@ pub const RuntimeEnv = struct {
|
|||
|
||||
if (callable.as_dynamic(Chunk.typeinfo)) |chunk_userdata| {
|
||||
const chunk = @as(*Chunk, @ptrCast(@alignCast(chunk_userdata)));
|
||||
const line = chunk.lines.values[chunk.cursor];
|
||||
|
||||
const chunk_name = try coral.utf8.alloc_formatted(self.allocator, "{name}@{line_number}", .{
|
||||
const chunk_name = try coral.utf8.alloc_formatted(self.allocator, "{name}@{line}", .{
|
||||
.name = get_name: {
|
||||
const string = name.as_string();
|
||||
|
||||
|
@ -658,7 +583,7 @@ pub const RuntimeEnv = struct {
|
|||
break: get_name string.?;
|
||||
},
|
||||
|
||||
.line_number = line.number,
|
||||
.line = chunk.lines.values[chunk.cursor],
|
||||
});
|
||||
|
||||
defer self.allocator.deallocate(chunk_name);
|
||||
|
@ -1088,8 +1013,6 @@ pub const Typeinfo = struct {
|
|||
};
|
||||
|
||||
pub fn assert(env: *RuntimeEnv, condition: bool) RuntimeError!void {
|
||||
coral.debug.assert(condition);
|
||||
|
||||
if (!condition) {
|
||||
return env.raise(error.IllegalState, "assertion", .{});
|
||||
}
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
const Expr = @import("./Expr.zig");
|
||||
|
||||
const Stmt = @import("./Stmt.zig");
|
||||
|
||||
const app = @import("../app.zig");
|
||||
|
||||
const coral = @import("coral");
|
||||
|
@ -6,8 +10,6 @@ const file = @import("../file.zig");
|
|||
|
||||
const kym = @import("../kym.zig");
|
||||
|
||||
const tokens = @import("./tokens.zig");
|
||||
|
||||
const tree = @import("./tree.zig");
|
||||
|
||||
name: *kym.RuntimeRef,
|
||||
|
@ -29,7 +31,7 @@ const Compiler = struct {
|
|||
chunk: *Self,
|
||||
env: *kym.RuntimeEnv,
|
||||
|
||||
fn compile_argument(self: *const Compiler, environment: *const tree.Environment, initial_argument: ?*const tree.Expr) kym.RuntimeError!u8 {
|
||||
fn compile_argument(self: *const Compiler, environment: *const tree.Environment, initial_argument: ?*const Expr) kym.RuntimeError!u8 {
|
||||
// TODO: Exceeding 255 arguments will make the VM crash.
|
||||
var maybe_argument = initial_argument;
|
||||
var argument_count = @as(u8, 0);
|
||||
|
@ -44,7 +46,7 @@ const Compiler = struct {
|
|||
return argument_count;
|
||||
}
|
||||
|
||||
fn compile_expression(self: *const Compiler, environment: *const tree.Environment, expression: *const tree.Expr, name: ?[]const coral.io.Byte) kym.RuntimeError!void {
|
||||
fn compile_expression(self: *const Compiler, environment: *const tree.Environment, expression: *const Expr, name: ?[]const coral.io.Byte) kym.RuntimeError!void {
|
||||
const number_format = coral.utf8.DecimalFormat{
|
||||
.delimiter = "_",
|
||||
.positive_prefix = .none,
|
||||
|
@ -73,24 +75,6 @@ const Compiler = struct {
|
|||
try self.chunk.write(expression.line, .{.push_const = try self.declare_string(literal)});
|
||||
},
|
||||
|
||||
.string_template => {
|
||||
var current_expression = expression.next orelse {
|
||||
return self.chunk.write(expression.line, .{.push_const = try self.declare_string("")});
|
||||
};
|
||||
|
||||
var component_count = @as(u8, 0);
|
||||
|
||||
while (true) {
|
||||
try self.compile_expression(environment, current_expression, null);
|
||||
|
||||
component_count += 1;
|
||||
|
||||
current_expression = current_expression.next orelse {
|
||||
return self.chunk.write(expression.line, .{.push_concat = component_count});
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
.symbol_literal => |literal| {
|
||||
try self.chunk.write(expression.line, .{.push_const = try self.declare_symbol(literal)});
|
||||
},
|
||||
|
@ -272,7 +256,7 @@ const Compiler = struct {
|
|||
}
|
||||
}
|
||||
|
||||
fn compile_statement(self: *const Compiler, environment: *const tree.Environment, initial_statement: *const tree.Stmt) kym.RuntimeError!*const tree.Stmt {
|
||||
fn compile_statement(self: *const Compiler, environment: *const tree.Environment, initial_statement: *const Stmt) kym.RuntimeError!*const Stmt {
|
||||
var current_statement = initial_statement;
|
||||
|
||||
while (true) {
|
||||
|
@ -461,7 +445,7 @@ const Compiler = struct {
|
|||
|
||||
const ConstList = coral.list.Stack(*kym.RuntimeRef);
|
||||
|
||||
const LineList = coral.list.Stack(tokens.Line);
|
||||
const LineList = coral.list.Stack(u32);
|
||||
|
||||
pub const Opcode = union (enum) {
|
||||
ret,
|
||||
|
@ -475,7 +459,6 @@ pub const Opcode = union (enum) {
|
|||
push_table: u32,
|
||||
push_builtin: Builtin,
|
||||
push_binding: u8,
|
||||
push_concat: u8,
|
||||
push_boxed,
|
||||
set_local: u8,
|
||||
get_dynamic,
|
||||
|
@ -557,10 +540,6 @@ pub fn dump(chunk: Self, env: *kym.RuntimeEnv) kym.RuntimeError!*kym.RuntimeRef
|
|||
.binding = push_binding,
|
||||
}),
|
||||
|
||||
.push_concat => |push_concat| coral.utf8.print_formatted(writer, "push concat ({count})\n", .{
|
||||
.count = push_concat,
|
||||
}),
|
||||
|
||||
.push_builtin => |push_builtin| coral.utf8.print_formatted(writer, "push builtin ({builtin})\n", .{
|
||||
.builtin = switch (push_builtin) {
|
||||
.import => "import",
|
||||
|
@ -691,33 +670,6 @@ pub fn execute(self: *Self, env: *kym.RuntimeEnv, frame: *const kym.Frame) kym.R
|
|||
try env.locals.push_one(if (self.bindings[push_binding]) |value| value.acquire() else null);
|
||||
},
|
||||
|
||||
.push_concat => |push_concat| {
|
||||
const frame_locals = env.locals.values[frame.locals_top ..];
|
||||
|
||||
try kym.assert(env, push_concat <= frame_locals.len);
|
||||
|
||||
const concat_locals = frame_locals[(frame_locals.len - push_concat) .. frame_locals.len];
|
||||
|
||||
for (concat_locals) |value| {
|
||||
_ = try env.expect(value);
|
||||
}
|
||||
|
||||
const concatenated_value = try env.concat(@ptrCast(concat_locals));
|
||||
|
||||
errdefer env.discard(concatenated_value);
|
||||
|
||||
var to_pop = concat_locals.len;
|
||||
|
||||
while (to_pop != 0) : (to_pop -= 1) {
|
||||
const popped = env.pop_local() catch unreachable;
|
||||
|
||||
coral.debug.assert(popped != null);
|
||||
env.discard(popped.?);
|
||||
}
|
||||
|
||||
try env.locals.push_one(concatenated_value);
|
||||
},
|
||||
|
||||
.bind => |bind| {
|
||||
const callable = try env.expect(try env.pop_local());
|
||||
|
||||
|
@ -1119,7 +1071,7 @@ fn typeinfo_to_string(env: *kym.RuntimeEnv, userdata: []coral.io.Byte) coral.io.
|
|||
return env.to_string(@as(*Self, @ptrCast(@alignCast(userdata))).name);
|
||||
}
|
||||
|
||||
pub fn write(self: *Self, line: tokens.Line, opcode: Opcode) coral.io.AllocationError!void {
|
||||
pub fn write(self: *Self, line: u32, opcode: Opcode) coral.io.AllocationError!void {
|
||||
try self.opcodes.push_one(opcode);
|
||||
try self.lines.push_one(line);
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@ const Stmt = @import("./Stmt.zig");
|
|||
|
||||
const coral = @import("coral");
|
||||
|
||||
const tokens = @import("../tokens.zig");
|
||||
const tokens = @import("./tokens.zig");
|
||||
|
||||
const tree = @import("../tree.zig");
|
||||
const tree = @import("./tree.zig");
|
||||
|
||||
next: ?*const Self = null,
|
||||
line: tokens.Line,
|
||||
line: u32,
|
||||
|
||||
kind: union (enum) {
|
||||
nil_literal,
|
||||
|
@ -15,7 +15,6 @@ kind: union (enum) {
|
|||
false_literal,
|
||||
number_literal: []const coral.io.Byte,
|
||||
string_literal: []const coral.io.Byte,
|
||||
string_template,
|
||||
symbol_literal: []const coral.io.Byte,
|
||||
table_construct: TableConstruct,
|
||||
key_value: KeyValue,
|
||||
|
@ -75,14 +74,14 @@ pub const BinaryOp = struct {
|
|||
stream.step();
|
||||
|
||||
if (stream.token == .end) {
|
||||
return root.report_error(stream.line, "expected other half of expression after `" ++ comptime token.text() ++ "`", .{});
|
||||
return root.report_error(stream, "expected other half of expression after `" ++ comptime token.text() ++ "`", .{});
|
||||
}
|
||||
|
||||
// TODO: Remove once Zig has fixed struct self-reassignment.
|
||||
const unnecessary_temp = expression;
|
||||
|
||||
expression = try root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.binary_op = .{
|
||||
|
@ -156,68 +155,6 @@ pub const TableConstruct = struct {
|
|||
entry: ?*const Self,
|
||||
};
|
||||
|
||||
const TemplateToken = union (enum) {
|
||||
invalid: []const coral.io.Byte,
|
||||
literal: []const coral.io.Byte,
|
||||
expression: []const coral.io.Byte,
|
||||
|
||||
fn extract(source: *[]const coral.io.Byte) ?TemplateToken {
|
||||
var cursor = @as(usize, 0);
|
||||
|
||||
defer source.* = source.*[cursor ..];
|
||||
|
||||
while (cursor < source.len) {
|
||||
switch (source.*[cursor]) {
|
||||
'{' => {
|
||||
cursor += 1;
|
||||
|
||||
while (true) : (cursor += 1) {
|
||||
if (cursor == source.len) {
|
||||
return .{.invalid = source.*[0 .. cursor]};
|
||||
}
|
||||
|
||||
if (source.*[cursor] == '}') {
|
||||
const token = TemplateToken{.expression = source.*[1 .. cursor]};
|
||||
|
||||
cursor += 1;
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
else => {
|
||||
cursor += 1;
|
||||
|
||||
while (true) : (cursor += 1) {
|
||||
if (cursor == source.len) {
|
||||
return .{.literal = source.*[0 .. cursor]};
|
||||
}
|
||||
|
||||
if (source.*[cursor] == '{') {
|
||||
const cursor_next = cursor + 1;
|
||||
|
||||
if (cursor_next == source.len) {
|
||||
return .{.invalid = source.*[0 .. cursor]};
|
||||
}
|
||||
|
||||
if (source.*[cursor_next] == '{') {
|
||||
cursor = cursor_next;
|
||||
|
||||
return .{.literal = source.*[0 .. cursor]};
|
||||
}
|
||||
|
||||
return .{.literal = source.*[0 .. cursor]};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
pub const UnaryOp = struct {
|
||||
operand: *Self,
|
||||
operation: Operation,
|
||||
|
@ -235,16 +172,16 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
stream.skip_newlines();
|
||||
|
||||
if (stream.token == .end) {
|
||||
return root.report_error(stream.line, "expected assignment after `=`", .{});
|
||||
return root.report_error(stream, "expected assignment after `=`", .{});
|
||||
}
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = switch (expression.kind) {
|
||||
.declaration_get => |declaration_get| convert: {
|
||||
if (declaration_get.declaration.is.readonly) {
|
||||
return root.report_error(stream.line, "readonly declarations cannot be re-assigned", .{});
|
||||
return root.report_error(stream, "readonly declarations cannot be re-assigned", .{});
|
||||
}
|
||||
|
||||
break: convert .{
|
||||
|
@ -271,7 +208,7 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
},
|
||||
},
|
||||
|
||||
else => return root.report_error(stream.line, "expected local or field on left-hand side of expression", .{}),
|
||||
else => return root.report_error(stream, "expected local or field on left-hand side of expression", .{}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
@ -307,13 +244,13 @@ fn parse_factor(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
const unnecessary_temp = expression;
|
||||
|
||||
expression = try root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.field_get = .{
|
||||
.identifier = switch (stream.token) {
|
||||
.identifier => |field_identifier| field_identifier,
|
||||
else => return root.report_error(stream.line, "expected identifier after `.`", .{}),
|
||||
else => return root.report_error(stream, "expected identifier after `.`", .{}),
|
||||
},
|
||||
|
||||
.object = unnecessary_temp,
|
||||
|
@ -331,7 +268,7 @@ fn parse_factor(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
const unnecessary_temp = expression;
|
||||
|
||||
expression = try root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.subscript_get = .{
|
||||
|
@ -342,14 +279,14 @@ fn parse_factor(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
});
|
||||
|
||||
if (stream.token != .symbol_bracket_right) {
|
||||
return root.report_error(stream.line, "expected closing `]` on subscript", .{});
|
||||
return root.report_error(stream, "expected closing `]` on subscript", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
},
|
||||
|
||||
.symbol_paren_left => {
|
||||
const lines_stepped = stream.line;
|
||||
const lines_stepped = stream.lines_stepped;
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
|
@ -364,7 +301,7 @@ fn parse_factor(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
switch (stream.token) {
|
||||
.symbol_comma => stream.skip_newlines(),
|
||||
.symbol_paren_right => break,
|
||||
else => return root.report_error(stream.line, "expected `,` or `)` after lambda argument", .{}),
|
||||
else => return root.report_error(stream, "expected `,` or `)` after lambda argument", .{}),
|
||||
}
|
||||
|
||||
const next_argument = try parse(root, stream, environment);
|
||||
|
@ -406,13 +343,13 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
const expression = try parse(root, stream, environment);
|
||||
|
||||
if (stream.token != .symbol_paren_right) {
|
||||
return root.report_error(stream.line, "expected a closing `)` after expression", .{});
|
||||
return root.report_error(stream, "expected a closing `)` after expression", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.group = expression},
|
||||
});
|
||||
},
|
||||
|
@ -421,7 +358,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .nil_literal,
|
||||
});
|
||||
},
|
||||
|
@ -430,7 +367,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .true_literal,
|
||||
});
|
||||
},
|
||||
|
@ -439,7 +376,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .false_literal,
|
||||
});
|
||||
},
|
||||
|
@ -448,7 +385,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.number_literal = value},
|
||||
});
|
||||
},
|
||||
|
@ -457,58 +394,50 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.string_literal = value},
|
||||
});
|
||||
},
|
||||
|
||||
.template_string => |value| {
|
||||
const line = stream.line;
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
return parse_template(root, value, line, environment);
|
||||
},
|
||||
|
||||
.symbol_at => {
|
||||
stream.step();
|
||||
|
||||
const identifier = switch (stream.token) {
|
||||
.identifier => |identifier| identifier,
|
||||
else => return root.report_error(stream.line, "expected identifier after `@`", .{}),
|
||||
else => return root.report_error(stream, "expected identifier after `@`", .{}),
|
||||
};
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
if (coral.io.are_equal(identifier, "import")) {
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .import_builtin,
|
||||
});
|
||||
}
|
||||
|
||||
if (coral.io.are_equal(identifier, "print")) {
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .print_builtin,
|
||||
});
|
||||
}
|
||||
|
||||
if (coral.io.are_equal(identifier, "vec2")) {
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .vec2_builtin,
|
||||
});
|
||||
}
|
||||
|
||||
if (coral.io.are_equal(identifier, "vec3")) {
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .vec3_builtin,
|
||||
});
|
||||
}
|
||||
|
||||
return root.report_error(stream.line, "unexpected identifier after `@`", .{});
|
||||
return root.report_error(stream, "unexpected identifier after `@`", .{});
|
||||
},
|
||||
|
||||
.symbol_period => {
|
||||
|
@ -516,13 +445,13 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
|
||||
const identifier = switch (stream.token) {
|
||||
.identifier => |identifier| identifier,
|
||||
else => return root.report_error(stream.line, "expected identifier after `.`", .{}),
|
||||
else => return root.report_error(stream, "expected identifier after `.`", .{}),
|
||||
};
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.symbol_literal = identifier},
|
||||
});
|
||||
},
|
||||
|
@ -531,12 +460,12 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.declaration_get = .{
|
||||
.declaration = (try environment.resolve_declaration(identifier)) orelse {
|
||||
return root.report_error(stream.line, "undefined identifier `{identifier}`", .{
|
||||
return root.report_error(stream, "undefined identifier `{identifier}`", .{
|
||||
.identifier = identifier,
|
||||
});
|
||||
}
|
||||
|
@ -549,7 +478,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
if (stream.token != .symbol_paren_left) {
|
||||
return root.report_error(stream.line, "expected `(` after opening lambda block", .{});
|
||||
return root.report_error(stream, "expected `(` after opening lambda block", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
@ -559,11 +488,11 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
while (stream.token != .symbol_paren_right) {
|
||||
const identifier = switch (stream.token) {
|
||||
.identifier => |identifier| identifier,
|
||||
else => return root.report_error(stream.line, "expected identifier", .{}),
|
||||
else => return root.report_error(stream, "expected identifier", .{}),
|
||||
};
|
||||
|
||||
_ = lambda_environment.declare_argument(identifier) catch |declare_error| {
|
||||
return root.report_declare_error(stream.line, identifier, declare_error);
|
||||
return root.report_declare_error(stream, identifier, declare_error);
|
||||
};
|
||||
|
||||
stream.skip_newlines();
|
||||
|
@ -571,14 +500,14 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
switch (stream.token) {
|
||||
.symbol_comma => stream.skip_newlines(),
|
||||
.symbol_paren_right => break,
|
||||
else => return root.report_error(stream.line, "expected `,` or `)` after identifier", .{}),
|
||||
else => return root.report_error(stream, "expected `,` or `)` after identifier", .{}),
|
||||
}
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
if (stream.token != .symbol_colon) {
|
||||
return root.report_error(stream.line, "expected `:` after closing `)` of lambda identifiers", .{});
|
||||
return root.report_error(stream, "expected `:` after closing `)` of lambda identifiers", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
@ -600,7 +529,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.lambda_construct = .{.environment = lambda_environment}},
|
||||
});
|
||||
},
|
||||
|
@ -610,7 +539,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
|
||||
if (stream.token == .symbol_brace_right) {
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.table_construct = .{.entry = null}},
|
||||
});
|
||||
}
|
||||
|
@ -632,13 +561,13 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
}
|
||||
|
||||
if (stream.token != .symbol_brace_right) {
|
||||
return root.report_error(stream.line, "expected closing `}` on table construct", .{});
|
||||
return root.report_error(stream, "expected closing `}` on table construct", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.table_construct = .{.entry = first_entry}},
|
||||
});
|
||||
},
|
||||
|
@ -647,7 +576,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.unary_op = .{
|
||||
|
@ -662,7 +591,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.unary_op = .{
|
||||
|
@ -673,7 +602,7 @@ fn parse_operand(root: *tree.Root, stream: *tokens.Stream, environment: *tree.En
|
|||
});
|
||||
},
|
||||
|
||||
else => return root.report_error(stream.line, "unexpected token in expression", .{}),
|
||||
else => return root.report_error(stream, "unexpected token in expression", .{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -684,26 +613,26 @@ fn parse_table_entry(root: *tree.Root, stream: *tokens.Stream, environment: *tre
|
|||
|
||||
const field = switch (stream.token) {
|
||||
.identifier => |identifier| identifier,
|
||||
else => return root.report_error(stream.line, "expected identifier in field symbol literal", .{}),
|
||||
else => return root.report_error(stream, "expected identifier in field symbol literal", .{}),
|
||||
};
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
if (stream.token != .symbol_equals) {
|
||||
return root.report_error(stream.line, "expected `=` after table symbol key", .{});
|
||||
return root.report_error(stream, "expected `=` after table symbol key", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.key_value = .{
|
||||
.value = try parse(root, stream, environment),
|
||||
|
||||
.key = try root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.symbol_literal = field},
|
||||
}),
|
||||
},
|
||||
|
@ -717,19 +646,19 @@ fn parse_table_entry(root: *tree.Root, stream: *tokens.Stream, environment: *tre
|
|||
const key = try parse(root, stream, environment);
|
||||
|
||||
if (stream.token != .symbol_bracket_right) {
|
||||
return root.report_error(stream.line, "expected `]` after subscript index expression", .{});
|
||||
return root.report_error(stream, "expected `]` after subscript index expression", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
if (stream.token != .symbol_equals) {
|
||||
return root.report_error(stream.line, "expected `=` after table expression key", .{});
|
||||
return root.report_error(stream, "expected `=` after table expression key", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
return root.create_expr(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.key_value = .{
|
||||
|
@ -744,45 +673,6 @@ fn parse_table_entry(root: *tree.Root, stream: *tokens.Stream, environment: *tre
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_template(root: *tree.Root, template: []const coral.io.Byte, line: tokens.Line, environment: *tree.Environment) tree.ParseError!*Self {
|
||||
const expression_head = try root.create_expr(.{
|
||||
.line = line,
|
||||
.kind = .string_template,
|
||||
});
|
||||
|
||||
var expression_tail = expression_head;
|
||||
var source = template;
|
||||
|
||||
while (TemplateToken.extract(&source)) |token| {
|
||||
const expression = try switch (token) {
|
||||
.invalid => |invalid| root.report_error(line, "invalid template format: `{invalid}`", .{
|
||||
.invalid = invalid,
|
||||
}),
|
||||
|
||||
.literal => |literal| root.create_expr(.{
|
||||
.line = line,
|
||||
.kind = .{.string_literal = literal},
|
||||
}),
|
||||
|
||||
.expression => |expression| create: {
|
||||
var stream = tokens.Stream{
|
||||
.source = expression,
|
||||
.line = line,
|
||||
};
|
||||
|
||||
stream.step();
|
||||
|
||||
break: create try parse(root, &stream, environment);
|
||||
},
|
||||
};
|
||||
|
||||
expression_tail.next = expression;
|
||||
expression_tail = expression;
|
||||
}
|
||||
|
||||
return expression_head;
|
||||
}
|
||||
|
||||
const parse_term = BinaryOp.parser(parse_factor, &.{
|
||||
.multiplication,
|
||||
.divsion,
|
|
@ -2,12 +2,12 @@ const Expr = @import("./Expr.zig");
|
|||
|
||||
const coral = @import("coral");
|
||||
|
||||
const tokens = @import("../tokens.zig");
|
||||
const tokens = @import("./tokens.zig");
|
||||
|
||||
const tree = @import("../tree.zig");
|
||||
const tree = @import("./tree.zig");
|
||||
|
||||
next: ?*const Self = null,
|
||||
line: tokens.Line,
|
||||
line: u32,
|
||||
|
||||
kind: union (enum) {
|
||||
top_expression: *const Expr,
|
||||
|
@ -46,17 +46,17 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
|
||||
if (stream.token != .end and stream.token != .newline) {
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.@"return" = .{.returned_expression = try Expr.parse(root, stream, environment)}},
|
||||
});
|
||||
}
|
||||
|
||||
if (stream.token != .end and stream.token != .newline) {
|
||||
return root.report_error(stream.line, "expected end or newline after return statement", .{});
|
||||
return root.report_error(stream, "expected end or newline after return statement", .{});
|
||||
}
|
||||
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.@"return" = .{.returned_expression = null}},
|
||||
});
|
||||
},
|
||||
|
@ -69,7 +69,7 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
const condition_expression = try Expr.parse(root, stream, environment);
|
||||
|
||||
if (stream.token != .symbol_colon) {
|
||||
return root.report_error(stream.line, "expected `:` after `while` statement", .{});
|
||||
return root.report_error(stream, "expected `:` after `while` statement", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
@ -88,7 +88,7 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
}
|
||||
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.@"while" = .{
|
||||
|
@ -106,13 +106,13 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
|
||||
const identifier = switch (stream.token) {
|
||||
.identifier => |identifier| identifier,
|
||||
else => return root.report_error(stream.line, "expected identifier after declaration", .{}),
|
||||
else => return root.report_error(stream, "expected identifier after declaration", .{}),
|
||||
};
|
||||
|
||||
stream.skip_newlines();
|
||||
|
||||
if (stream.token != .symbol_equals) {
|
||||
return root.report_error(stream.line, "expected `=` after declaration `{identifier}`", .{
|
||||
return root.report_error(stream, "expected `=` after declaration `{identifier}`", .{
|
||||
.identifier = identifier,
|
||||
});
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.declare = .{
|
||||
|
@ -129,12 +129,12 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
.declaration = declare: {
|
||||
if (is_constant) {
|
||||
break: declare environment.declare_constant(identifier) catch |declaration_error| {
|
||||
return root.report_declare_error(stream.line, identifier, declaration_error);
|
||||
return root.report_declare_error(stream, identifier, declaration_error);
|
||||
};
|
||||
}
|
||||
|
||||
break: declare environment.declare_variable(identifier) catch |declaration_error| {
|
||||
return root.report_declare_error(stream.line, identifier, declaration_error);
|
||||
return root.report_declare_error(stream, identifier, declaration_error);
|
||||
};
|
||||
},
|
||||
},
|
||||
|
@ -145,7 +145,7 @@ pub fn parse(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Enviro
|
|||
.keyword_if => return parse_branch(root, stream, environment),
|
||||
|
||||
else => return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
.kind = .{.top_expression = try Expr.parse(root, stream, environment)},
|
||||
}),
|
||||
}
|
||||
|
@ -157,7 +157,7 @@ fn parse_branch(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
const expression = try Expr.parse(root, stream, environment);
|
||||
|
||||
if (stream.token != .symbol_colon) {
|
||||
return root.report_error(stream.line, "expected `:` after `{token}`", .{.token = stream.token.text()});
|
||||
return root.report_error(stream, "expected `:` after `{token}`", .{.token = stream.token.text()});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
@ -171,7 +171,7 @@ fn parse_branch(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.@"if" = .{
|
||||
|
@ -187,7 +187,7 @@ fn parse_branch(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
stream.step();
|
||||
|
||||
if (stream.token != .symbol_colon) {
|
||||
return root.report_error(stream.line, "expected `:` after `if` statement condition", .{});
|
||||
return root.report_error(stream, "expected `:` after `if` statement condition", .{});
|
||||
}
|
||||
|
||||
stream.skip_newlines();
|
||||
|
@ -205,7 +205,7 @@ fn parse_branch(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
stream.skip_newlines();
|
||||
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.@"if" = .{
|
||||
|
@ -219,7 +219,7 @@ fn parse_branch(root: *tree.Root, stream: *tokens.Stream, environment: *tree.Env
|
|||
|
||||
.keyword_elif => {
|
||||
return root.create_stmt(.{
|
||||
.line = stream.line,
|
||||
.line = stream.lines_stepped,
|
||||
|
||||
.kind = .{
|
||||
.@"if" = .{
|
|
@ -1,9 +1,5 @@
|
|||
const coral = @import("coral");
|
||||
|
||||
pub const Line = struct {
|
||||
number: u32,
|
||||
};
|
||||
|
||||
pub const Token = union(enum) {
|
||||
end,
|
||||
unknown: coral.io.Byte,
|
||||
|
@ -35,7 +31,6 @@ pub const Token = union(enum) {
|
|||
|
||||
number: []const coral.io.Byte,
|
||||
string: []const coral.io.Byte,
|
||||
template_string: []const coral.io.Byte,
|
||||
|
||||
keyword_nil,
|
||||
keyword_false,
|
||||
|
@ -86,7 +81,6 @@ pub const Token = union(enum) {
|
|||
|
||||
.number => |literal| literal,
|
||||
.string => |literal| literal,
|
||||
.template_string => |literal| literal,
|
||||
|
||||
.keyword_const => "const",
|
||||
.keyword_nil => "nil",
|
||||
|
@ -109,7 +103,7 @@ pub const Token = union(enum) {
|
|||
|
||||
pub const Stream = struct {
|
||||
source: []const coral.io.Byte,
|
||||
line: Line = .{.number = 1},
|
||||
lines_stepped: u32 = 1,
|
||||
token: Token = .newline,
|
||||
|
||||
pub fn skip_newlines(self: *Stream) void {
|
||||
|
@ -140,7 +134,7 @@ pub const Stream = struct {
|
|||
'\n' => {
|
||||
cursor += 1;
|
||||
self.token = .newline;
|
||||
self.line.number += 1;
|
||||
self.lines_stepped += 1;
|
||||
|
||||
return;
|
||||
},
|
||||
|
@ -311,22 +305,6 @@ pub const Stream = struct {
|
|||
return;
|
||||
},
|
||||
|
||||
'`' => {
|
||||
cursor += 1;
|
||||
|
||||
const begin = cursor;
|
||||
|
||||
while (cursor < self.source.len) switch (self.source[cursor]) {
|
||||
'`' => break,
|
||||
else => cursor += 1,
|
||||
};
|
||||
|
||||
self.token = .{.template_string = self.source[begin .. cursor]};
|
||||
cursor += 1;
|
||||
|
||||
return;
|
||||
},
|
||||
|
||||
'"' => {
|
||||
cursor += 1;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
pub const Expr = @import("./tree/Expr.zig");
|
||||
const Expr = @import("./Expr.zig");
|
||||
|
||||
pub const Stmt = @import("./tree/Stmt.zig");
|
||||
const Stmt = @import("./Stmt.zig");
|
||||
|
||||
const coral = @import("coral");
|
||||
|
||||
|
@ -163,22 +163,22 @@ pub const Root = struct {
|
|||
|
||||
const MessageList = coral.list.Stack([]coral.io.Byte);
|
||||
|
||||
pub fn report_error(self: *Root, line: tokens.Line, comptime format: []const u8, args: anytype) ParseError {
|
||||
pub fn report_error(self: *Root, stream: *tokens.Stream, comptime format: []const u8, args: anytype) ParseError {
|
||||
const allocator = self.arena.as_allocator();
|
||||
|
||||
try self.error_messages.push_one(try coral.utf8.alloc_formatted(allocator, "{line_number}: {message}", .{
|
||||
try self.error_messages.push_one(try coral.utf8.alloc_formatted(allocator, "{line}: {message}", .{
|
||||
.message = try coral.utf8.alloc_formatted(allocator, format, args),
|
||||
.line_number = line.number,
|
||||
.line = stream.lines_stepped,
|
||||
}));
|
||||
|
||||
return error.BadSyntax;
|
||||
}
|
||||
|
||||
pub fn report_declare_error(self: *Root, line: tokens.Line, identifier: []const coral.io.Byte, @"error": Environment.DeclareError) ParseError {
|
||||
pub fn report_declare_error(self: *Root, stream: *tokens.Stream, identifier: []const coral.io.Byte, @"error": Environment.DeclareError) ParseError {
|
||||
return switch (@"error") {
|
||||
error.OutOfMemory => error.OutOfMemory,
|
||||
|
||||
error.DeclarationExists => self.report_error(line, "declaration `{identifier}` already exists", .{
|
||||
error.DeclarationExists => self.report_error(stream, "declaration `{identifier}` already exists", .{
|
||||
.identifier = identifier,
|
||||
}),
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue