summaryrefslogtreecommitdiff
path: root/src/tokenize.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2025-02-17 18:59:01 -0600
committerNic Gaffney <gaffney_nic@protonmail.com>2025-02-17 18:59:01 -0600
commitc77c9a42cd048a9f68aedf4fed5bfa8a2a4051d6 (patch)
tree09476066d0ec079a731db734272f99eeb7452aa7 /src/tokenize.zig
parent013086f9c55e33f7bfcfd775c8b9d03fb2311de4 (diff)
downloadcalico-c77c9a42cd048a9f68aedf4fed5bfa8a2a4051d6.tar.gz
Order of operatoins and basic if statement
Diffstat (limited to 'src/tokenize.zig')
-rw-r--r--src/tokenize.zig49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/tokenize.zig b/src/tokenize.zig
index 770f483..6fc0ebc 100644
--- a/src/tokenize.zig
+++ b/src/tokenize.zig
@@ -11,6 +11,7 @@ pub const TokenType = enum {
ident,
intLit,
// Keywords
+ ifstmt,
constant,
variable,
exit,
@@ -22,6 +23,9 @@ pub const TokenType = enum {
slash,
semiCol,
equal,
+ eqleql,
+ lessthan,
+ greaterthan,
// Symbols
openBrace,
closeBrace,
@@ -35,6 +39,7 @@ pub const Token = union(TokenType) {
ident: []const u8,
intLit: i32,
// Keywords
+ ifstmt,
constant,
variable,
exit,
@@ -46,6 +51,9 @@ pub const Token = union(TokenType) {
slash,
semiCol,
equal,
+ eqleql,
+ lessthan,
+ greaterthan,
// Symbols
openBrace,
closeBrace,
@@ -53,6 +61,35 @@ pub const Token = union(TokenType) {
closeParen,
arrow,
+ pub fn toTokenType(self: Token) TokenType {
+ switch (self) {
+ .ident => .ident,
+ .intLit => .intLit,
+ // Keywords
+ .ifstmt => .ifstmt,
+ .constant => .constant,
+ .variable => .variable,
+ .exit => .exit,
+ .fun => .fun,
+ // => Operators .=>
+ .plus => .plus,
+ .minus => .minus,
+ .star => .star,
+ .slash => .slash,
+ .semiCol => .semiCol,
+ .equal => .equal,
+ .eqleql => .eqleql,
+ .lessthan => .lessthan,
+ .greaterthan => .greaterthan,
+ // => Symbols .=>
+ .openBrace => .openBrace,
+ .closeBrace => .closeBrace,
+ .openParen => .openParen,
+ .closeParen => .closeParen,
+ .arrow => .arrow,
+ }
+ }
+
pub fn fromChar(char: u8) !Token {
return switch (char) {
'+' => .plus,
@@ -65,6 +102,8 @@ pub const Token = union(TokenType) {
'}' => .closeBrace,
'(' => .openParen,
')' => .closeParen,
+ '<' => .lessthan,
+ '>' => .greaterthan,
else => TokenizeError.UnknownToken,
};
}
@@ -75,6 +114,7 @@ pub const Token = union(TokenType) {
if (eql(u8, str, "const")) return .constant;
if (eql(u8, str, "var")) return .variable;
if (eql(u8, str, "fn")) return .fun;
+ if (eql(u8, str, "if")) return .ifstmt;
return Token{ .ident = str };
}
};
@@ -160,6 +200,15 @@ pub const Tokenizer = struct {
while (self.src.peek()) |char| {
try switch (char) {
+ '=' => {
+ self.src.skip();
+ if (self.src.peek().? != '=') {
+ try self.toks.append(.equal);
+ continue;
+ }
+ self.src.skip();
+ try self.toks.append(.eqleql);
+ },
'-' => {
self.src.skip();
if (self.src.peek().? != '>') {