Compare commits
No commits in common. "5b940c9e445cb9c8e1f1a69082c1e942a9fe3d07" and "9b43095f86baecb447cd0a3a3582bd88786752f4" have entirely different histories.
5b940c9e44
...
9b43095f86
@ -1,52 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const t = std.testing;
|
|
||||||
|
|
||||||
const TokenType = enum {
|
|
||||||
Int,
|
|
||||||
Float,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Token = struct {
|
|
||||||
value: []const u8,
|
|
||||||
token_type: TokenType,
|
|
||||||
start_pos: usize,
|
|
||||||
|
|
||||||
pub fn init(value: []const u8, token_type: TokenType, start: usize) Token {
|
|
||||||
return Token{
|
|
||||||
.value = value,
|
|
||||||
.token_type = token_type,
|
|
||||||
.start_pos = start,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn tokenize(input: []const u8) !void {
|
|
||||||
const next_token = try number(input, 0);
|
|
||||||
_ = next_token;
|
|
||||||
|
|
||||||
std.debug.print("tokenize :D {s}\n", .{input});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn number(input: []const u8, start: usize) !?Token {
|
|
||||||
const first_char = input[start];
|
|
||||||
if (!is_digit(first_char)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Token.init(input[start .. start + 1], TokenType.Int, start);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_digit(c: u8) bool {
|
|
||||||
return '0' <= c and c <= '9';
|
|
||||||
}
|
|
||||||
|
|
||||||
test "number lexer" {
|
|
||||||
const input = "3";
|
|
||||||
const result = try number(input, 0);
|
|
||||||
|
|
||||||
if (result) |r| {
|
|
||||||
try std.testing.expectEqual("3", r.value);
|
|
||||||
} else {
|
|
||||||
try std.testing.expect(false);
|
|
||||||
}
|
|
||||||
}
|
|
17
src/main.zig
17
src/main.zig
@ -1,10 +1,11 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lexic = @import("./01_lexic/root.zig");
|
|
||||||
|
|
||||||
const thp_version: []const u8 = "0.0.0";
|
const thp_version: []const u8 = "0.0.0";
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
try repl();
|
try repl();
|
||||||
|
|
||||||
|
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||||
|
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn repl() !void {
|
fn repl() !void {
|
||||||
@ -13,18 +14,6 @@ fn repl() !void {
|
|||||||
const stdout = bw.writer();
|
const stdout = bw.writer();
|
||||||
try stdout.print("The THP REPL, v{s}\n", .{thp_version});
|
try stdout.print("The THP REPL, v{s}\n", .{thp_version});
|
||||||
try stdout.print("Enter expressions to evaluate. Enter CTRL-D to exit.\n", .{});
|
try stdout.print("Enter expressions to evaluate. Enter CTRL-D to exit.\n", .{});
|
||||||
try bw.flush();
|
|
||||||
|
|
||||||
const stdin = std.io.getStdIn().reader();
|
|
||||||
|
|
||||||
try stdout.print("\nthp => ", .{});
|
|
||||||
try bw.flush();
|
|
||||||
|
|
||||||
const bare_line = try stdin.readUntilDelimiterAlloc(std.heap.page_allocator, '\n', 8192);
|
|
||||||
defer std.heap.page_allocator.free(bare_line);
|
|
||||||
const line = std.mem.trim(u8, bare_line, "\r");
|
|
||||||
|
|
||||||
try lexic.tokenize(line);
|
|
||||||
|
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user