Skip to content
68 changes: 1 addition & 67 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,75 +1,9 @@
.{
// This is the default name used by packages depending on this one. For
// example, when a user runs `zig fetch --save <url>`, this field is used
// as the key in the `dependencies` table. Although the user can choose a
// different name, most users will stick with this provided value.
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = .math_pl,
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
// Together with name, this represents a globally unique package
// identifier. This field is generated by the Zig toolchain when the
// package is first created, and then *never changes*. This allows
// unambiguous detection of one package being an updated version of
// another.
//
// When forking a Zig project, this id should be regenerated (delete the
// field and run `zig build`) if the upstream project is still maintained.
// Otherwise, the fork is *hostile*, attempting to take control over the
// original project's identity. Thus it is recommended to leave the comment
// on the following line intact, so that it shows up in code reviews that
// modify the field.
.fingerprint = 0x4abe8a4f673a8d3c, // Changing this has security and trust implications.
// Tracks the earliest Zig version that the package considers to be a
// supported use case.
.minimum_zig_version = "0.15.2",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
// // `hash`, otherwise you are communicating that you expect to find the old hash at
// // the new URL. If the contents of a URL change this will result in a hash mismatch
// // which will prevent zig from using it.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",
//
// // When this is set to `true`, a package is declared to be lazily
// // fetched. This makes the dependency only get fetched if it is
// // actually used.
// .lazy = false,
//},
},
// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package. Only files listed here will remain on disk
// when using the zig package manager. As a rule of thumb, one should list
// files required for compilation plus any license(s).
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
2 changes: 1 addition & 1 deletion examples/string.sub
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print_str(""" Hello "sub" """,);
print_str("""Hello "sub\\"\nI'm a string""""",);
45 changes: 27 additions & 18 deletions src/eval.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");

const expression_pkg = @import("expression.zig");
const stdlib_pkg = @import("stdlib.zig");
Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn sema(program: Expr, ctx: Context) void {
sema(expr.lhs.*, ctx);
sema(expr.rhs.*, ctx);
},
.constant, .str => {},
.constant => {},
}
},
.bool_ => |bool_expr| {
Expand Down Expand Up @@ -108,7 +109,7 @@ pub fn sema(program: Expr, ctx: Context) void {
sema(if_expr.then.*, ctx);
sema(if_expr.else_.*, ctx);
},
.struct_, .var_, .void_ => {},
.struct_, .var_, .void_, .str => {},
}
}

Expand All @@ -126,7 +127,10 @@ fn assert_of_type(value: Expr, ok: bool) void {
value.cursor.row,
value.cursor.col,
});
std.process.exit(1);
if (builtin.mode != .Debug)
std.process.exit(1)
else
panic("", .{});
}
}

Expand Down Expand Up @@ -166,7 +170,7 @@ fn add_structs(program: Expr, ctx: *Context) !void {
try add_structs(arith_expr.lhs.*, ctx);
try add_structs(arith_expr.rhs.*, ctx);
},
.constant, .str => {},
.constant => {},
}
},
.bool_ => |expr| {
Expand All @@ -186,6 +190,7 @@ fn add_structs(program: Expr, ctx: *Context) !void {
try add_structs(sub_expr, ctx);
}
},
.str => {},
// if you need to add structs for more elements,
// you can finish implement switch for all other types of expr
else => {},
Expand Down Expand Up @@ -245,6 +250,7 @@ pub fn eval(expr: Expr, ctx: Context, local_vars: *Vars) Expr {
},
.bind => |bind| return eval_bind(bind, ctx, local_vars),
.void_ => return expr,
.str => return expr,
}
}

Expand All @@ -259,7 +265,7 @@ fn eval_bind(bind: BindExpr, ctx: Context, local_vars: *Vars) Expr {
else if (ebody.is_struct_instance())
.{ .struct_instance = ebody.as.struct_instance }
else if (ebody.is_str())
.{ .str = ebody.as.arith.str }
.{ .str = ebody.as.str }
else if (ebody.is_void())
.{ .void_ = {} }
else
Expand All @@ -286,13 +292,16 @@ fn eval_fn_call(expr: FnCallExpr, ctx: Context, local_vars: *Vars, cursor: Curso
const arg = expr.args.items[i];
const arg_name = fn_def.args.items[i];
const arg_eval = eval(arg, ctx, local_vars);
assert_of_type(arg_eval, arg_eval.tag() == .bool_ or arg_eval.tag() == .arith);
assert_of_type(
arg_eval,
arg_eval.tag() == .bool_ or arg_eval.tag() == .arith or arg_eval.tag() == .str,
);
const var_: Var = if (arg_eval.tag() == .bool_)
.{ .bool_ = arg_eval.as.bool_.constant }
else if (arg_eval.tag() == .arith and arg_eval.as.arith.tag() == .constant)
.{ .int = arg_eval.as.arith.constant }
else if (arg_eval.tag() == .arith and arg_eval.as.arith.tag() == .str)
.{ .str = arg_eval.as.arith.str }
else if (arg_eval.tag() == .str)
.{ .str = arg_eval.as.str }
else
panic("unhandled case", .{});
fn_params.putNoClobber(arg_name, var_) catch unreachable;
Expand Down Expand Up @@ -337,7 +346,7 @@ fn eval_var(expr: []const u8, ctx: Context, local_vars: *Vars, cursor: Cursor) E
.file_path = ctx.file_path,
},
.str => |var_| .{
.as = .{ .arith = .{ .str = var_ } },
.as = .{ .str = var_ },
.content = ctx.content,
.cursor = cursor,
.file_path = ctx.file_path,
Expand Down Expand Up @@ -388,32 +397,32 @@ fn eval_arith(expr: ArithExpr, ctx: Context, local_vars: *Vars, cursor: Cursor)
assert_of_type(rhs, rhs.as.arith == .constant);
return switch (expr) {
.prod => .{
.as = .{ .arith = .{ .constant = lhs.as.arith.constant * rhs.as.arith.constant } },
.as = .{ .arith = .{
.constant = lhs.as.arith.constant * rhs.as.arith.constant,
} },
.content = ctx.content,
.cursor = cursor,
.file_path = ctx.file_path,
},
.plus => .{
.as = .{ .arith = .{ .constant = lhs.as.arith.constant + rhs.as.arith.constant } },
.as = .{ .arith = .{
.constant = lhs.as.arith.constant + rhs.as.arith.constant,
} },
.content = ctx.content,
.cursor = cursor,
.file_path = ctx.file_path,
},
.minus => .{
.as = .{ .arith = .{ .constant = lhs.as.arith.constant - rhs.as.arith.constant } },
.as = .{ .arith = .{
.constant = lhs.as.arith.constant - rhs.as.arith.constant,
} },
.content = ctx.content,
.cursor = cursor,
.file_path = ctx.file_path,
},
else => unreachable,
};
},
.str => |str| return .{
.as = .{ .arith = .{ .str = str } },
.content = ctx.content,
.cursor = cursor,
.file_path = ctx.file_path,
},
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/expression.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub const ExprTag = enum {
struct_,
struct_instance,
field_access,
str,
void_,
};

Expand Down Expand Up @@ -203,6 +204,15 @@ pub const Expr = struct {
};
}

pub fn create_str(str: []const u8, l: *const Lexer) Expr {
return .{
.as = .{ .str = str },
.cursor = l.previous_cursor,
.content = l.content,
.file_path = l.file_path,
};
}

pub fn create_bool(op: BoolExpr, l: *const Lexer) Expr {
return .{
.as = .{ .bool_ = op },
Expand Down Expand Up @@ -262,7 +272,7 @@ pub const Expr = struct {

pub fn is_str(self: Self) bool {
if (self.tag() != .arith) return false;
return self.as.arith.tag() == .str;
return self.tag() == .str;
}

pub fn is_bool(self: Self) bool {
Expand Down Expand Up @@ -295,6 +305,7 @@ pub const Expr = struct {
}
},
.void_ => std.debug.print("void", .{}),
.str => |expr| std.debug.print("{s}", .{expr}),
.struct_ => |expr| expr.print(),
.bind => |expr| expr.print(),
}
Expand All @@ -319,6 +330,7 @@ const ExprAs = union(ExprTag) {
struct_: StructExpr,
struct_instance: StructInstanceExpr,
field_access: FieldAccessExpr,
str: []const u8,
void_: void,
};

Expand Down Expand Up @@ -408,14 +420,12 @@ pub const IfExpr = struct {
std.debug.print("elseif (", .{});
eval.print();
std.debug.print(") ", .{});
// std.debug.print("{{", .{});
then.print();
std.debug.print("\n", .{});
}

std.debug.print("else ", .{});
self.else_.print();
// std.debug.print("", .{});
}
};

Expand All @@ -424,7 +434,6 @@ const ArithTag = enum {
minus,
plus,
constant,
str,
};

pub const ArithExpr = union(ArithTag) {
Expand All @@ -434,15 +443,13 @@ pub const ArithExpr = union(ArithTag) {
minus: BinOp,
plus: BinOp,
constant: i32,
str: []const u8,

pub fn print(self: Self) void {
switch (self) {
.prod => |expr| expr.print("*"),
.minus => |expr| expr.print("-"),
.plus => |expr| expr.print("+"),
.constant => |expr| std.debug.print("{}", .{expr}),
.str => |expr| std.debug.print("s\"{s}\"", .{expr}),
}
}

Expand Down Expand Up @@ -503,7 +510,6 @@ pub const BoolExpr = union(BoolTag) {
.constant => |expr| std.debug.print("{}", .{expr}),
.eql => |expr| {
expr.print("==");
// std.debug.print(")", .{});
},
.gt => |expr| {
expr.print("<");
Expand Down
10 changes: 4 additions & 6 deletions src/lexer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -331,21 +331,19 @@ pub const Lexer = struct {
count += 1;
_ = l.next_char();
} else {
// print("extend here???\n", .{});
// l.name.extend();
break;
}
}
// print("pos here: {}, count: {}, delimiter: {}\n", .{ l.cursor.pos, count, nb_delimiter });
if (count != nb_delimiter) {
if (count < nb_delimiter) {
// This is not the end of the string
// so we account the "" as in the string
// print("name: {s}\n", .{l.name.asStr(l.content)});
for (0..count) |_| {
l.name.extend();
}
} else {
l.name.extend();
const nb_extend = count - nb_delimiter + 1;
for (0..nb_extend) |_|
l.name.extend();
break :loop;
}
} else l.name.extend();
Expand Down
Loading
Loading