save current lexer impl

This commit is contained in:
2026-01-03 15:02:26 +01:00
parent 6176d549c1
commit 629b65e151
4 changed files with 17 additions and 25 deletions

7
example.ll Normal file
View File

@@ -0,0 +1,7 @@
label main:
b0:
%1 = mul i32 3, i32 3
%2 = add %1, i32 1
param %2
%3 = call putchar

View File

@@ -1,18 +1,3 @@
extern putchar
fn hello() {
local h = 72
putchar(h)
local e = h - 3
putchar(e)
local l = h + 4
putchar(l)
putchar(l)
local o = 100 - 21
putchar(o)
}
fn main() { fn main() {
hello()
putchar(3 * 3 + 1) putchar(3 * 3 + 1)
} }

View File

@@ -80,7 +80,7 @@ public:
} }
default: default:
// fprintf(stderr, "%s:%d:%d: ERROR: unexpected token while parsing %ld\n", m_lexer->filename(), token->line_number, token->offset_start, token->token); // fprintf(stderr, "%s:%d:%d: ERROR: unexpected token while parsing %ld\n", m_lexer->filename(), token->line_number, token->offset_start, token->token);
ErrorLogger::Raise(Error::ParseError(m_lexer->filename(), StringView::FromFormat("unexpected token while parsing '%c'", token->token), token->line_number, token->offset_start)); ErrorLogger::Raise(Error::ParseError(m_lexer->filename(), StringView::FromFormat("unexpected token while parsing '%d'", token->token), token->line_number, token->offset_start));
break; break;
} }
@@ -179,7 +179,7 @@ public:
case TokenType::Fn: program->PushFunction(ParseFnDecl()); break; case TokenType::Fn: program->PushFunction(ParseFnDecl()); break;
case TokenType::Extern: program->PushExtern(ParseExtern()); break; case TokenType::Extern: program->PushExtern(ParseExtern()); break;
default: { default: {
ErrorLogger::Raise(Error::ParseError(m_lexer->filename(), StringView::FromFormat("unexpected token while parsing '%c'", token.token), token.line_number, token.offset_start)); ErrorLogger::Raise(Error::ParseError(m_lexer->filename(), StringView::FromFormat("unexpected token while parsing '%d'", token.token), token.line_number, token.offset_start));
assert(false); assert(false);
} }
break; break;

View File

@@ -73,7 +73,7 @@ public:
auto lnl = m_last_newline; auto lnl = m_last_newline;
if (!NextToken()) if (!NextToken())
{ {
return nullptr; return new Token(TokenType::Eof, m_line, m_pos - m_last_newline, m_pos - m_last_newline);
} }
auto seeked = m_token; auto seeked = m_token;
m_token = s; m_token = s;
@@ -92,11 +92,11 @@ public:
public: public:
bool NextToken() bool NextToken()
{ {
// if (m_pos >= m_code.len()) if (m_pos > m_code.len())
// { {
// m_token = Token(TokenType::Eof); m_token = Token(TokenType::Eof);
// return false; return false;
// } }
char c = m_code.data[m_pos++]; char c = m_code.data[m_pos++];
@@ -109,7 +109,7 @@ public:
c = m_code.data[m_pos++]; c = m_code.data[m_pos++];
} }
if (m_pos >= m_code.len()) if (m_pos-1 > m_code.len())
{ {
m_token = Token(TokenType::Eof); m_token = Token(TokenType::Eof);
return false; return false;
@@ -126,7 +126,7 @@ public:
s.Push(m_code.data[m_pos++]); s.Push(m_code.data[m_pos++]);
} }
s.Push('\0'); s.Push('\0');
m_token = Token(TokenType::Id, m_line, offset_start, m_pos - m_last_newline); m_token = Token(TokenType::Id, m_line, offset_start, offset_start);
m_token.string = s.view(); m_token.string = s.view();
if (strcmp("extern", m_token.string.c_str()) == 0) if (strcmp("extern", m_token.string.c_str()) == 0)