diff --git a/example.ll b/example.ll new file mode 100644 index 0000000..2212dd0 --- /dev/null +++ b/example.ll @@ -0,0 +1,7 @@ +label main: +b0: + %1 = mul i32 3, i32 3 + %2 = add %1, i32 1 + param %2 + %3 = call putchar + diff --git a/example.rx b/example.rx index e5db68f..6dd3718 100644 --- a/example.rx +++ b/example.rx @@ -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() { - hello() putchar(3 * 3 + 1) } \ No newline at end of file diff --git a/include/parser/ast.hpp b/include/parser/ast.hpp index 8eafbb4..a8f944a 100644 --- a/include/parser/ast.hpp +++ b/include/parser/ast.hpp @@ -80,7 +80,7 @@ public: } default: // 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; } @@ -179,7 +179,7 @@ public: case TokenType::Fn: program->PushFunction(ParseFnDecl()); break; case TokenType::Extern: program->PushExtern(ParseExtern()); break; 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); } break; diff --git a/include/parser/lexer.hpp b/include/parser/lexer.hpp index dffa776..06a0e33 100644 --- a/include/parser/lexer.hpp +++ b/include/parser/lexer.hpp @@ -73,7 +73,7 @@ public: auto lnl = m_last_newline; 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; m_token = s; @@ -92,11 +92,11 @@ public: public: bool NextToken() { - // if (m_pos >= m_code.len()) - // { - // m_token = Token(TokenType::Eof); - // return false; - // } + if (m_pos > m_code.len()) + { + m_token = Token(TokenType::Eof); + return false; + } char c = m_code.data[m_pos++]; @@ -109,7 +109,7 @@ public: c = m_code.data[m_pos++]; } - if (m_pos >= m_code.len()) + if (m_pos-1 > m_code.len()) { m_token = Token(TokenType::Eof); return false; @@ -126,7 +126,7 @@ public: s.Push(m_code.data[m_pos++]); } 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(); if (strcmp("extern", m_token.string.c_str()) == 0)