Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / lexer.re
diff options
context:
space:
mode:
Commita7cd9d4f8d83024402bacb39786e5e1a018a0a04 (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-14 23:45:56 +0200
Committer
 Martin Dobrev <martindobrev0@gmail.com>
2018-11-14 23:45:56 +0200
Files1f99c6f8d00d9b94b69c8f3525643344a32d7414 /spasm/src/asm/lexer.re
IPL-a7cd9d4f8d83024402bacb39786e5e1a018a0a04.tar
IPL-a7cd9d4f8d83024402bacb39786e5e1a018a0a04.tar.gz
IPL-a7cd9d4f8d83024402bacb39786e5e1a018a0a04.zip
Parent9a7a8a31e75d1b85274ef46ca29714270062809e (diff)
Spasm can assemble programs on with the new instruction set (#47)
* Use checkout as-is, commit as-is * Spasm can assemble programs based on the new instruction set
1 files changed, 103 insertions, 86 deletions
-rw-r--r--spasm/src/asm/lexer.re189
diff --git a/spasm/src/asm/lexer.re b/spasm/src/asm/lexer.re
index c54d54f..298e340 100644
--- a/spasm/src/asm/lexer.re
+++ b/spasm/src/asm/lexer.re
@@ -6,29 +6,29 @@
namespace SpasmImpl
{
- namespace ASM
- {
- //! Namespace for all lexical analisys classes
- namespace Lexer
- {
+namespace ASM
+{
+//! Namespace for all lexical analisys classes
+namespace Lexer
+{
- Lexer::Lexer (std::istream & _file, size_t _buffer_size)
- : file (&_file), buffer_size (_buffer_size), state (unsigned(-1)),
- lineno (0)
- {
- buffer = new char[buffer_size];
- cursor = limit = marker = token_start = NULL;
- }
+Lexer::Lexer (std::istream & _file, size_t _buffer_size)
+ : file (&_file), buffer_size (_buffer_size), state (unsigned(-1)),
+ lineno (0)
+{
+ buffer = new char[buffer_size];
+ cursor = limit = marker = token_start = NULL;
+}
- Lexer::~Lexer ()
- {
- delete [] buffer;
- }
+Lexer::~Lexer ()
+{
+ delete [] buffer;
+}
- bool Lexer::tokenize (TokenStream &ts)
- {
+bool Lexer::tokenize (TokenStream &ts)
+{
/*!re2c
-re2c:indent:top = 5;
+re2c:indent:top = 1;
re2c:define:YYFILL="return true;";
re2c:define:YYFILL:naked=1;
@@ -56,7 +56,7 @@ XINTEGER = "0" [xX] XDIGIT+ ;
IDENTIFIER = [a-zA-Z_] [0-9a-zA-Z_]* ;
INTEGER {
- ts.push_token (Token (Token::integer, lineno,
+ ts.push_token (Token (Token::Integer, lineno,
token_start, cursor));
token_start = cursor;
@@ -64,7 +64,7 @@ INTEGER {
}
XINTEGER {
- ts.push_token (Token (Token::xinteger, lineno,
+ ts.push_token (Token (Token::XInteger, lineno,
token_start, cursor));
token_start = cursor;
@@ -72,132 +72,149 @@ XINTEGER {
}
"push" {
- ts.push_token (Token (Token::push, lineno));
+ ts.push_token (Token (Token::Push, lineno));
token_start = cursor;
continue;
}
+"alloc" {
+ ts.push_token (Token (Token::Alloc, lineno));
+ token_start = cursor;
+ continue;
+ }
"pop" {
- ts.push_token (Token (Token::pop, lineno));
+ ts.push_token (Token (Token::Pop, lineno));
+ token_start = cursor;
+
+ continue;
+ }
+"popto" {
+ ts.push_token (Token (Token::PopTo, lineno));
token_start = cursor;
continue;
}
"dup" {
- ts.push_token (Token (Token::dup, lineno));
+ ts.push_token (Token (Token::Dup, lineno));
token_start = cursor;
continue;
}
"read" {
- ts.push_token (Token (Token::read, lineno));
+ ts.push_token (Token (Token::Read, lineno));
token_start = cursor;
continue;
}
"print" {
- ts.push_token (Token (Token::print, lineno));
+ ts.push_token (Token (Token::Print, lineno));
token_start = cursor;
continue;
}
"add" {
- ts.push_token (Token (Token::plus, lineno));
+ ts.push_token (Token (Token::Add, lineno));
token_start = cursor;
continue;
}
"sub" {
- ts.push_token (Token (Token::minus, lineno));
+ ts.push_token (Token (Token::Sub, lineno));
token_start = cursor;
continue;
}
"mul" {
- ts.push_token (Token (Token::multiply, lineno));
+ ts.push_token (Token (Token::Mul, lineno));
token_start = cursor;
continue;
}
"div" {
- ts.push_token (Token (Token::divide, lineno));
+ ts.push_token (Token (Token::Div, lineno));
token_start = cursor;
continue;
}
"mod" {
- ts.push_token (Token (Token::modulus, lineno));
+ ts.push_token (Token (Token::Mod, lineno));
token_start = cursor;
continue;
}
"jmpt" {
- ts.push_token (Token (Token::jmpt, lineno));
+ ts.push_token (Token (Token::JumpT, lineno));
token_start = cursor;
continue;
}
"jmpf" {
- ts.push_token (Token (Token::jmpf, lineno));
+ ts.push_token (Token (Token::JumpF, lineno));
token_start = cursor;
continue;
}
"jmp" {
- ts.push_token (Token (Token::jmp, lineno));
+ ts.push_token (Token (Token::Jump, lineno));
+ token_start = cursor;
+
+ continue;
+ }
+"const" {
+ ts.push_token (Token (Token::Const, lineno));
token_start = cursor;
continue;
}
"call" {
- ts.push_token (Token (Token::call, lineno));
+ ts.push_token (Token (Token::Call, lineno));
token_start = cursor;
continue;
}
"ret" {
- ts.push_token (Token (Token::ret, lineno));
+ ts.push_token (Token (Token::Ret, lineno));
token_start = cursor;
continue;
}
"leq" {
- ts.push_token (Token (Token::lesseq, lineno));
+ ts.push_token (Token (Token::LessEq, lineno));
token_start = cursor;
continue;
}
"less" {
- ts.push_token (Token (Token::less, lineno));
+ ts.push_token (Token (Token::Less, lineno));
token_start = cursor;
continue;
}
"label" {
- ts.push_token (Token (Token::label, lineno));
+ ts.push_token (Token (Token::Label, lineno));
token_start = cursor;
continue;
}
IDENTIFIER {
- ts.push_token (Token (Token::ident, lineno, token_start,
+ ts.push_token (Token (Token::Ident, lineno, token_start,
cursor));
token_start = cursor;
@@ -218,7 +235,7 @@ IDENTIFIER {
}
"\000" {
- ts.push_token (Token (Token::endinput, lineno));
+ ts.push_token (Token (Token::EndInput, lineno));
break;
}
@@ -232,63 +249,63 @@ IDENTIFIER {
return true;
}
- /*!
- ** Fills up the buffer when neccessary.
- ** Note the buffer is always filled to the maximum, not just the
- ** number of bytes neccessary for the lexer to continue.
- **
- ** \param nbytes - the number of bytes
- */
+/*!
+** Fills up the buffer when neccessary.
+** Note the buffer is always filled to the maximum, not just the
+** number of bytes neccessary for the lexer to continue.
+**
+** \param nbytes - the number of bytes
+*/
- void Lexer::buffer_grow (size_t new_size)
- {
- char *new_buffer = new char[new_size];
+void Lexer::buffer_grow (size_t new_size)
+{
+ char *new_buffer = new char[new_size];
- std::copy (buffer, limit, new_buffer);
+ std::copy (buffer, limit, new_buffer);
- token_start = new_buffer + (token_start - buffer);
- cursor = new_buffer + (cursor - buffer);
- limit = new_buffer + new_size;
+ token_start = new_buffer + (token_start - buffer);
+ cursor = new_buffer + (cursor - buffer);
+ limit = new_buffer + new_size;
- buffer_size = new_size;
+ buffer_size = new_size;
- delete [] buffer;
- buffer = new_buffer;
- }
+ delete [] buffer;
+ buffer = new_buffer;
+}
- void Lexer::read (size_t nbytes)
- {
- if (token_start + nbytes > limit) { // we need larger buffer
- buffer_grow (token_start - buffer + nbytes);
- }
+void Lexer::read (size_t nbytes)
+{
+ if (token_start + nbytes > limit) { // we need larger buffer
+ buffer_grow (token_start - buffer + nbytes);
+ }
- std::copy (token_start, limit, buffer);
- cursor -= token_start - buffer;
- char *old_limit = limit - (token_start - buffer);
- token_start = buffer;
+ std::copy (token_start, limit, buffer);
+ cursor -= token_start - buffer;
+ char *old_limit = limit - (token_start - buffer);
+ token_start = buffer;
- file->read (old_limit, limit - old_limit);
- if (file->eof ()) {
- size_t bytes = file->gcount ();
- std::fill (old_limit + bytes, limit, '\0');
- }
- }
+ file->read (old_limit, limit - old_limit);
+ if (file->eof ()) {
+ size_t bytes = file->gcount ();
+ std::fill (old_limit + bytes, limit, '\0');
+ }
+}
- void Lexer::buffer_init ()
- {
- file->read (buffer, buffer_size);
- if (file->eof ()) {
- size_t bytes = file->gcount ();
- std::fill (buffer + bytes, buffer + buffer_size, '\0');
- }
+void Lexer::buffer_init ()
+{
+ file->read (buffer, buffer_size);
+ if (file->eof ()) {
+ size_t bytes = file->gcount ();
+ std::fill (buffer + bytes, buffer + buffer_size, '\0');
+ }
- cursor = marker = token_start = buffer;
- limit = buffer + buffer_size;
- }
+ cursor = marker = token_start = buffer;
+ limit = buffer + buffer_size;
+}
- } // namespace Lexer
+} // namespace Lexer
- } // namespace ASM
+} // namespace ASM
} // namespace SpasmImpl