Skip to content

Commit

Permalink
Rename AST to Ast
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiisbored committed Jul 8, 2023
1 parent 9e981cf commit 1f86323
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
48 changes: 24 additions & 24 deletions src/Parser.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const std = @import("std");

const Scanner = @import("./Scanner.zig");
const AST = @import("./ast.zig").AST;
const Ast = @import("./ast.zig").Ast;

const Self = @This();

arena: std.heap.ArenaAllocator,

scanner: *Scanner,
routines: *std.ArrayList(AST.Program.Routine),
routines: *std.ArrayList(Ast.Program.Routine),

current: Scanner.Token,
previous: Scanner.Token,
Expand All @@ -23,7 +23,7 @@ pub const Error = error{
pub fn init(
allocator: std.mem.Allocator,
scanner: *Scanner,
routines: *std.ArrayList(AST.Program.Routine),
routines: *std.ArrayList(Ast.Program.Routine),
) Self {
const initToken = Scanner.Token{
.type = .eof,
Expand Down Expand Up @@ -105,33 +105,33 @@ fn match(self: *Self, @"type": Scanner.TokenType) Error!bool {

// == GRAMMAR == //

fn whileBlock(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn whileBlock(self: *Self, target: *std.ArrayList(Ast)) Error!void {
const allocator = self.arena.allocator();

try self.advance();

var commands = std.ArrayList(AST).init(allocator);
var commands = std.ArrayList(Ast).init(allocator);

while (!self.check(.@"while")) {
try self.command(&commands);
}

try self.consume(.@"while", "Expected WHILE");

try target.append(AST{ .@"while" = commands.items });
try target.append(Ast{ .@"while" = commands.items });
}

fn ifBlock(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn ifBlock(self: *Self, target: *std.ArrayList(Ast)) Error!void {
const allocator = self.arena.allocator();

try self.advance();

var if_true = std.ArrayList(AST).init(allocator);
var if_true = std.ArrayList(Ast).init(allocator);
while (!(self.check(.then) or self.check(.@"else"))) {
try self.command(&if_true);
}

var if_false = std.ArrayList(AST).init(allocator);
var if_false = std.ArrayList(Ast).init(allocator);

if (try self.match(.@"else")) {
while (!(self.check(.then))) {
Expand All @@ -142,21 +142,21 @@ fn ifBlock(self: *Self, target: *std.ArrayList(AST)) Error!void {
try self.consume(.then, "Expected THEN");

try target.append(
AST{
.@"if" = AST.If{
Ast{
.@"if" = Ast.If{
.if_true = if_true.items,
.if_false = if_false.items,
},
},
);
}

fn number(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn number(self: *Self, target: *std.ArrayList(Ast)) Error!void {
try self.advance();
try target.append(AST{ .n = std.fmt.parseInt(i32, self.previous.str, 10) catch unreachable });
try target.append(Ast{ .n = std.fmt.parseInt(i32, self.previous.str, 10) catch unreachable });
}

fn arithmetic(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn arithmetic(self: *Self, target: *std.ArrayList(Ast)) Error!void {
try self.advance();
try target.append(
switch (self.previous.type) {
Expand All @@ -169,28 +169,28 @@ fn arithmetic(self: *Self, target: *std.ArrayList(AST)) Error!void {
);
}

fn boolean(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn boolean(self: *Self, target: *std.ArrayList(Ast)) Error!void {
try self.advance();
try target.append(
switch (self.previous.type) {
.true => AST{ .b = true },
.false => AST{ .b = false },
.true => Ast{ .b = true },
.false => Ast{ .b = false },
else => unreachable,
},
);
}

fn string(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn string(self: *Self, target: *std.ArrayList(Ast)) Error!void {
try self.advance();
try target.append(AST{ .s = self.previous.str[1 .. self.previous.str.len - 1] });
try target.append(Ast{ .s = self.previous.str[1 .. self.previous.str.len - 1] });
}

fn identifier(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn identifier(self: *Self, target: *std.ArrayList(Ast)) Error!void {
try self.advance();
try target.append(AST{ .id = self.previous.str });
try target.append(Ast{ .id = self.previous.str });
}

fn command(self: *Self, target: *std.ArrayList(AST)) Error!void {
fn command(self: *Self, target: *std.ArrayList(Ast)) Error!void {
switch (self.current.type) {
.do => try self.whileBlock(target),
.@"if" => try self.ifBlock(target),
Expand All @@ -209,14 +209,14 @@ fn routine(self: *Self) Error!void {
try self.consume(.routine, "Expected routine");

const routine_name = self.previous.str;
var commands = std.ArrayList(AST).init(allocator);
var commands = std.ArrayList(Ast).init(allocator);

while (!(self.check(.routine) or self.check(.eof))) {
try self.command(&commands);
}

try self.routines.append(
AST.Program.Routine{
Ast.Program.Routine{
.name = routine_name,
.ast = commands.items,
},
Expand Down
14 changes: 7 additions & 7 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ const std = @import("std");

const Vm = @import("./Vm.zig");

pub const AST = union(enum) {
pub const Ast = union(enum) {
n: i32,
b: bool,
s: []const u8,
id: []const u8,
@"if": If,
@"while": []const AST,
@"while": []const Ast,
add,
sub,
div,
mul,

pub const If = struct {
if_true: []const AST,
if_false: []const AST,
if_true: []const Ast,
if_false: []const Ast,
};

pub const Program = struct {
Expand All @@ -25,7 +25,7 @@ pub const AST = union(enum) {

pub const Routine = struct {
name: []const u8,
ast: []const AST,
ast: []const Ast,

pub fn print(self: Routine, writer: anytype) !void {
try std.fmt.format(writer, "(routine '{s}' (", .{self.name});
Expand Down Expand Up @@ -64,7 +64,7 @@ pub const AST = union(enum) {
}
};

pub fn print(self: AST, writer: anytype) !void {
pub fn print(self: Ast, writer: anytype) !void {
try switch (self) {
.n => |n| std.fmt.format(writer, "(n {})", .{n}),
.b => |b| std.fmt.format(writer, "(b {})", .{b}),
Expand Down Expand Up @@ -106,7 +106,7 @@ pub const AST = union(enum) {

fn compile(
instructions: *std.ArrayList(Vm.Instruction),
routine: []const AST,
routine: []const Ast,
) !void {
for (routine) |c| {
switch (c) {
Expand Down
8 changes: 4 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const print = std.debug.print;

const Vm = @import("./Vm.zig");
const AST = @import("./ast.zig").AST;
const Ast = @import("./ast.zig").Ast;
const Scanner = @import("./Scanner.zig");
const Parser = @import("./Parser.zig");
const debug = @import("./constants.zig").debug;
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn main() !void {
print("=== SOURCE ===\n{s}\n", .{source});
}

var routines = std.ArrayList(AST.Program.Routine).init(allocator);
var routines = std.ArrayList(Ast.Program.Routine).init(allocator);
defer routines.deinit();

var scanner = Scanner.init(source);
Expand Down Expand Up @@ -92,10 +92,10 @@ pub fn main() !void {
print("=== TO BYTECODE ===\n", .{});
}

var program = try AST.Program.init(allocator, &vm, routines.items);
var program = try Ast.Program.init(allocator, &vm, routines.items);
defer program.deinit();

// We don't need the AST anymore.
// We don't need the Ast anymore.
parser.deinit();

if (debug) {
Expand Down

0 comments on commit 1f86323

Please sign in to comment.