diff options
| Commit | 4fca8e8e6c1056aed11437c87e9175232b48c8f4 (patch) | |
|---|---|---|
| Author | 2018-11-22 16:04:22 +0200 | |
| Committer | 2018-11-22 16:04:22 +0200 | |
| Files | bda85506ae253afd73d3ab36d3685b4e5bae3847 /spasm/src/asm | |
| IPL-4fca8e8e6c1056aed11437c87e9175232b48c8f4.tar IPL-4fca8e8e6c1056aed11437c87e9175232b48c8f4.tar.gz IPL-4fca8e8e6c1056aed11437c87e9175232b48c8f4.zip | ||
| Parent | 75faff8b35b8f9bfa0a00b973e9323c1dc4c614e (diff) | |
| Parent | ddc50fe944df0a33386a3213c8021f07ac8ffdd9 (diff) | |
Merge pull request #62 from SofiaCPP/feature/double
Feature/double
8 files changed, 837 insertions, 590 deletions
| -rw-r--r-- | spasm/src/asm/assembler.cpp | 108 | ||||
| -rw-r--r-- | spasm/src/asm/assembler.hpp | 2 | ||||
| -rw-r--r-- | spasm/src/asm/bytecode.cpp | 52 | ||||
| -rw-r--r-- | spasm/src/asm/bytecode.hpp | 9 | ||||
| -rw-r--r-- | spasm/src/asm/lexer.cpp | 795 | ||||
| -rw-r--r-- | spasm/src/asm/lexer.re | 418 | ||||
| -rw-r--r-- | spasm/src/asm/token.cpp | 33 | ||||
| -rw-r--r-- | spasm/src/asm/token.hpp | 10 |
diff --git a/spasm/src/asm/assembler.cpp b/spasm/src/asm/assembler.cpp index fbe6675..fadd747 100644 --- a/spasm/src/asm/assembler.cpp +++ b/spasm/src/asm/assembler.cpp @@ -14,6 +14,54 @@ Assembler::Assembler(Lexer::Tokenizer& tokenizer, Bytecode_Stream& bytecode) { } +int get_arg_size(const Lexer::Token args[]) +{ + int size = 0; + for (int i = 0; i < 3; ++i) + { + switch (args[i].type()) + { + case Lexer::Token::Integer: + case Lexer::Token::XInteger: + { + if (args[i].value_int() > 0xffffffff) + { + size = std::max(size, 3); + } + else if (args[i].value_int() > 0xffff) + { + size = std::max(size, 2); + } + else if (args[i].value_int() > 0xff) + { + size = std::max(size, 1); + } + else if (args[i].value_int() < -0xffffffffLL) + { + size = std::max(size, 3); + } + else if (args[i].value_int() < -0xffff) + { + size = std::max(size, 2); + } + else if (args[i].value_int() < -0xff) + { + size = std::max(size, 1); + } + break; + } + case Lexer::Token::FloatingPoint: + { + size = std::max(size, 3); + break; + } + default: + break; + } + } + return size; +} + void Assembler::assemble() { Lexer::Token token = _tokenizer->next_token(); @@ -39,42 +87,64 @@ void Assembler::assemble() } else { - _bytecode->push_opcode((Bytecode_Stream::Opcode_t)token.type()); + Lexer::Token args[3]; if (type >= Lexer::Token::_OneArgBegin) { + args[0] = _tokenizer->next_token(); + } + if (type >= Lexer::Token::_TwoArgBegin) + { + args[1] = _tokenizer->next_token(); + } + if (type >= Lexer::Token::_ThreeArgBegin) + { + args[2] = _tokenizer->next_token(); + } + const auto size = get_arg_size(args); + _bytecode->push_opcode( + (Bytecode_Stream::Opcode_t)((size << 6) | token.type())); + const auto arg_size = 1 << size; + + if (args[0].type() != Lexer::Token::NotUsed) + { if (type == Lexer::Token::Call || type == Lexer::Token::Jump) { - token = _tokenizer->next_token(); - assert(token.type() == Lexer::Token::Ident); - assemble_identifier(token); + assert(args[0].type() == Lexer::Token::Ident); + assemble_identifier(args[0]); } else { - token = _tokenizer->next_token(); - assert(token.type() == Lexer::Token::Integer); - _bytecode->push_integer(token.value_int()); + assert(args[0].type() == Lexer::Token::Integer); + _bytecode->push_integer(args[0].value_int(), arg_size); } } - if (type >= Lexer::Token::_TwoArgBegin) + if (args[1].type() != Lexer::Token::NotUsed) { if (type == Lexer::Token::JumpF || type == Lexer::Token::JumpT) { - token = _tokenizer->next_token(); - assert(token.type() == Lexer::Token::Ident); - assemble_identifier(token); + assert(args[1].type() == Lexer::Token::Ident); + assemble_identifier(args[1]); } else { - token = _tokenizer->next_token(); - assert(token.type() == Lexer::Token::Integer); - _bytecode->push_integer(token.value_int()); + if (args[1].type() == Lexer::Token::Integer) + { + _bytecode->push_integer(args[1].value_int(), arg_size); + } + else if (args[1].type() == Lexer::Token::FloatingPoint) + { + _bytecode->push_double(args[1].value_double()); + } + else + { + assert(false && "expected a number type"); + } } } - if (type >= Lexer::Token::_ThreeArgBegin) + if (args[2].type() != Lexer::Token::NotUsed) { - token = _tokenizer->next_token(); - assert(token.type() == Lexer::Token::Integer); - _bytecode->push_integer(token.value_int()); + assert(args[2].type() == Lexer::Token::Integer); + _bytecode->push_integer(args[2].value_int(), arg_size); } } @@ -105,7 +175,7 @@ void Assembler::assemble_identifier(const Lexer::Token& token) _bytecode->push_location(symbol->definition()); } -bool compile(std::istream& istr, Bytecode_Memory& bytecode) +bool compile(std::istream& istr, Bytecode_Stream& bytecode) { Lexer::Tokenizer tokenizer(istr); Assembler assembler(tokenizer, bytecode); diff --git a/spasm/src/asm/assembler.hpp b/spasm/src/asm/assembler.hpp index d29139b..90e8815 100644 --- a/spasm/src/asm/assembler.hpp +++ b/spasm/src/asm/assembler.hpp @@ -29,7 +29,7 @@ class Assembler }; // class Assembler -bool compile(std::istream&, Bytecode_Memory& bytecode); +bool compile(std::istream&, Bytecode_Stream& bytecode); } // namespace ASM } // namespace SpasmImpl diff --git a/spasm/src/asm/bytecode.cpp b/spasm/src/asm/bytecode.cpp index 39f1de6..cc250ba 100644 --- a/spasm/src/asm/bytecode.cpp +++ b/spasm/src/asm/bytecode.cpp @@ -26,17 +26,29 @@ void Bytecode_File::push_opcode(Bytecode_Stream::Opcode_t opcode) push_byte(opcode); } -void Bytecode_File::push_integer(int number) +union DoubleToBits { + double as_double; + int64_t as_bits; +}; + +int64_t to_bits(double d) { - if (number <= 0xff) - { - push_byte((byte)number); - } - else - { - for (size_t i = 0; i < sizeof(int); ++i) - push_byte((number >> (i << 3)) & 0xff); - } + DoubleToBits v; + v.as_double = d; + return v.as_bits; +}; + +void Bytecode_File::push_integer(int64_t number, int size) +{ + for (int i = 0; i < size; ++i) + push_byte((number >> (i << 3)) & 0xff); +} + +void Bytecode_File::push_double(double number) +{ + auto bits = to_bits(number); + for (size_t i = 0; i < sizeof(size_t); ++i) + push_byte((bits >> (i << 3)) & 0xff); } void Bytecode_Memory::push_byte(Bytecode_Stream::byte byte) @@ -49,17 +61,17 @@ void Bytecode_Memory::push_opcode(Bytecode_Stream::Opcode_t opcode) push_byte(opcode); } -void Bytecode_Memory::push_integer(int number) +void Bytecode_Memory::push_integer(int64_t number, int size) { - if (number <= 0xff) - { - push_byte((byte)number); - } - else - { - for (size_t i = 0; i < sizeof(int); ++i) - push_byte((number >> (i << 3)) & 0xff); - } + for (int i = 0; i < size; ++i) + push_byte((number >> (i << 3)) & 0xff); +} + +void Bytecode_Memory::push_double(double number) +{ + auto bits = to_bits(number); + for (size_t i = 0; i < sizeof(size_t); ++i) + push_byte((bits >> (i << 3)) & 0xff); } void Bytecode_Memory::set_location(size_t index, size_t location) diff --git a/spasm/src/asm/bytecode.hpp b/spasm/src/asm/bytecode.hpp index 374ccef..fb80122 100644 --- a/spasm/src/asm/bytecode.hpp +++ b/spasm/src/asm/bytecode.hpp @@ -18,7 +18,8 @@ class Bytecode_Stream virtual ~Bytecode_Stream(); virtual void push_opcode(Opcode_t) = 0; - virtual void push_integer(int) = 0; + virtual void push_integer(int64_t, int size) = 0; + virtual void push_double(double) = 0; virtual void push_location(size_t) = 0; virtual void set_location(size_t, size_t) = 0; virtual size_t size() const = 0; @@ -30,7 +31,8 @@ class Bytecode_File : public Bytecode_Stream Bytecode_File(const std::string&); virtual void push_opcode(Opcode_t); - virtual void push_integer(int); + virtual void push_integer(int64_t, int size) override; + virtual void push_double(double) override; private: Bytecode_File(const Bytecode_File&); @@ -45,7 +47,8 @@ class Bytecode_Memory : public Bytecode_Stream { public: virtual void push_opcode(Opcode_t); - virtual void push_integer(int); + virtual void push_integer(int64_t, int) override; + virtual void push_double(double) override; virtual void push_location(size_t); virtual void set_location(size_t, size_t); virtual size_t size() const; diff --git a/spasm/src/asm/lexer.cpp b/spasm/src/asm/lexer.cpp index 3af83c7..5be7836 100644 --- a/spasm/src/asm/lexer.cpp +++ b/spasm/src/asm/lexer.cpp @@ -1,4 +1,4 @@ -/* Generated by re2c 1.1.1 on Thu Nov 15 10:01:26 2018 */ +/* Generated by re2c 1.1.1 on Thu Nov 22 14:23:32 2018 */ #line 1 "lexer.re" #include <iostream> @@ -41,15 +41,16 @@ bool Lexer::tokenize(TokenStream& ts) { case 0x00: goto yy2; - case '\t': - case ' ': - goto yy6; case '\n': - goto yy9; - case '-': + goto yy6; + case ' ': + goto yy8; + case '#': goto yy11; + case '-': + goto yy13; case '0': - goto yy12; + goto yy14; case '1': case '2': case '3': @@ -59,7 +60,9 @@ bool Lexer::tokenize(TokenStream& ts) case '7': case '8': case '9': - goto yy14; + goto yy16; + case ':': + goto yy18; case 'A': case 'B': case 'C': @@ -104,111 +107,136 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; case 'a': - goto yy19; + goto yy23; case 'c': - goto yy20; + goto yy24; case 'd': - goto yy21; + goto yy25; case 'j': - goto yy22; + goto yy26; case 'l': - goto yy23; + goto yy27; case 'm': - goto yy24; + goto yy28; case 'p': - goto yy25; + goto yy29; case 'r': - goto yy26; + goto yy30; case 's': - goto yy27; + goto yy31; default: goto yy4; } yy2: ++cursor; -#line 254 "lexer.re" +#line 274 "lexer.re" { ts.push_token(Token(Token::EndInput, lineno)); break; } -#line 125 "lexer.cpp" +#line 126 "lexer.cpp" yy4: ++cursor; yy5: -#line 260 "lexer.re" +#line 280 "lexer.re" { return false; } -#line 133 "lexer.cpp" +#line 134 "lexer.cpp" yy6: ++cursor; +#line 267 "lexer.re" + { + ++lineno; + token_start = cursor; + + continue; + } +#line 144 "lexer.cpp" + yy8: + ++cursor; if (limit <= cursor) return true; yych = *cursor; switch (yych) { - case '\t': case ' ': - goto yy6; - default: goto yy8; + default: + goto yy10; } - yy8: -#line 241 "lexer.re" + yy10: +#line 251 "lexer.re" { token_start = cursor; continue; } -#line 150 "lexer.cpp" - yy9: +#line 160 "lexer.cpp" + yy11: ++cursor; -#line 247 "lexer.re" + if (limit <= cursor) + return true; + yych = *cursor; + switch (yych) { - ++lineno; - token_start = cursor; - - continue; + case '\n': + goto yy32; + default: + goto yy11; } -#line 160 "lexer.cpp" - yy11: + yy13: yych = *++cursor; switch (yych) { case '0': - goto yy28; + goto yy34; default: goto yy5; } - yy12: + yy14: yych = *(marker = ++cursor); switch (yych) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case ':': + goto yy36; case 'X': case 'x': - goto yy29; + goto yy38; default: - goto yy13; + goto yy15; } - yy13: -#line 59 "lexer.re" + yy15: +#line 60 "lexer.re" { ts.push_token(Token(Token::Integer, lineno, token_start, cursor)); token_start = cursor; continue; } -#line 183 "lexer.cpp" - yy14: - ++cursor; - if (limit <= cursor) +#line 202 "lexer.cpp" + yy16: + marker = ++cursor; + if ((limit - cursor) < 2) return true; yych = *cursor; switch (yych) { + case '.': + goto yy39; case '0': case '1': case '2': @@ -219,16 +247,27 @@ bool Lexer::tokenize(TokenStream& ts) case '7': case '8': case '9': - goto yy14; + goto yy16; + case ':': + goto yy18; default: - goto yy13; + goto yy15; } - yy16: + yy18: + ++cursor; +#line 256 "lexer.re" + { + token_start = cursor; + + continue; + } +#line 230 "lexer.cpp" + yy20: ++cursor; if (limit <= cursor) return true; yych = *cursor; - yy17: + yy21: switch (yych) { case '0': @@ -294,25 +333,25 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy18; + goto yy22; } - yy18: -#line 233 "lexer.re" + yy22: +#line 243 "lexer.re" { ts.push_token(Token(Token::Ident, lineno, token_start, cursor)); token_start = cursor; continue; } -#line 281 "lexer.cpp" - yy19: +#line 311 "lexer.cpp" + yy23: yych = *++cursor; switch (yych) { case '0': - goto yy31; + goto yy40; case '1': case '2': case '3': @@ -322,84 +361,84 @@ bool Lexer::tokenize(TokenStream& ts) case '7': case '8': case '9': - goto yy33; + goto yy42; case 'd': - goto yy35; + goto yy44; default: - goto yy17; + goto yy21; } - yy20: + yy24: yych = *++cursor; switch (yych) { case 'a': - goto yy36; + goto yy45; case 'o': - goto yy37; + goto yy46; default: - goto yy17; + goto yy21; } - yy21: + yy25: yych = *++cursor; switch (yych) { case 'i': - goto yy38; + goto yy47; case 'u': - goto yy39; + goto yy48; default: - goto yy17; + goto yy21; } - yy22: + yy26: yych = *++cursor; switch (yych) { case 'm': - goto yy40; + goto yy49; default: - goto yy17; + goto yy21; } - yy23: + yy27: yych = *++cursor; switch (yych) { case 'a': - goto yy41; + goto yy50; case 'e': - goto yy42; + goto yy51; default: - goto yy17; + goto yy21; } - yy24: + yy28: yych = *++cursor; switch (yych) { case 'o': - goto yy43; + goto yy52; case 'u': - goto yy44; + goto yy53; default: - goto yy17; + goto yy21; } - yy25: + yy29: yych = *++cursor; switch (yych) { case 'o': - goto yy45; + goto yy54; case 'r': - goto yy46; + goto yy55; case 'u': - goto yy47; + goto yy56; default: - goto yy17; + goto yy21; } - yy26: + yy30: yych = *++cursor; switch (yych) { case '0': - goto yy48; + goto yy57; case '1': case '2': case '3': @@ -409,25 +448,62 @@ bool Lexer::tokenize(TokenStream& ts) case '7': case '8': case '9': - goto yy50; + goto yy59; case 'e': - goto yy52; + goto yy61; default: - goto yy17; + goto yy21; } - yy27: + yy31: yych = *++cursor; switch (yych) { case 'u': - goto yy53; + goto yy62; default: - goto yy17; + goto yy21; } - yy28: + yy32: ++cursor; - goto yy13; - yy29: +#line 261 "lexer.re" + { + ++lineno; + token_start = cursor; + + continue; + } +#line 401 "lexer.cpp" + yy34: + ++cursor; + goto yy15; + yy35: + ++cursor; + if (limit <= cursor) + return true; + yych = *cursor; + yy36: + switch (yych) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + goto yy35; + case ':': + goto yy18; + default: + goto yy37; + } + yy37: + cursor = marker; + goto yy15; + yy38: yych = *++cursor; switch (yych) { @@ -453,14 +529,30 @@ bool Lexer::tokenize(TokenStream& ts) case 'd': case 'e': case 'f': - goto yy54; + goto yy63; default: - goto yy30; + goto yy37; } - yy30: - cursor = marker; - goto yy13; - yy31: + yy39: + yych = *++cursor; + switch (yych) + { + case '0': + goto yy66; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + goto yy68; + default: + goto yy37; + } + yy40: yych = *++cursor; switch (yych) { @@ -527,12 +619,12 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy32; + goto yy41; } - yy32: -#line 74 "lexer.re" + yy41: +#line 84 "lexer.re" { Token t(Token::Integer, lineno, token_start + 1, cursor); t.set_int(-t.value_int()); @@ -540,8 +632,8 @@ bool Lexer::tokenize(TokenStream& ts) token_start = cursor; continue; } -#line 473 "lexer.cpp" - yy33: +#line 547 "lexer.cpp" + yy42: ++cursor; if (limit <= cursor) return true; @@ -558,7 +650,7 @@ bool Lexer::tokenize(TokenStream& ts) case '7': case '8': case '9': - goto yy33; + goto yy42; case 'A': case 'B': case 'C': @@ -612,130 +704,130 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy32; + goto yy41; } - yy35: + yy44: yych = *++cursor; switch (yych) { case 'd': - goto yy57; + goto yy70; default: - goto yy17; + goto yy21; } - yy36: + yy45: yych = *++cursor; switch (yych) { case 'l': - goto yy59; + goto yy72; default: - goto yy17; + goto yy21; } - yy37: + yy46: yych = *++cursor; switch (yych) { case 'n': - goto yy60; + goto yy73; default: - goto yy17; + goto yy21; } - yy38: + yy47: yych = *++cursor; switch (yych) { case 'v': - goto yy61; + goto yy74; default: - goto yy17; + goto yy21; } - yy39: + yy48: yych = *++cursor; switch (yych) { case 'p': - goto yy63; + goto yy76; default: - goto yy17; + goto yy21; } - yy40: + yy49: yych = *++cursor; switch (yych) { case 'p': - goto yy65; + goto yy78; default: - goto yy17; + goto yy21; } - yy41: + yy50: yych = *++cursor; switch (yych) { case 'b': - goto yy67; + goto yy80; default: - goto yy17; + goto yy21; } - yy42: + yy51: yych = *++cursor; switch (yych) { case 'q': - goto yy68; + goto yy81; case 's': - goto yy70; + goto yy83; default: - goto yy17; + goto yy21; } - yy43: + yy52: yych = *++cursor; switch (yych) { case 'd': - goto yy71; + goto yy84; default: - goto yy17; + goto yy21; } - yy44: + yy53: yych = *++cursor; switch (yych) { case 'l': - goto yy73; + goto yy86; default: - goto yy17; + goto yy21; } - yy45: + yy54: yych = *++cursor; switch (yych) { case 'p': - goto yy75; + goto yy88; default: - goto yy17; + goto yy21; } - yy46: + yy55: yych = *++cursor; switch (yych) { case 'i': - goto yy77; + goto yy90; default: - goto yy17; + goto yy21; } - yy47: + yy56: yych = *++cursor; switch (yych) { case 's': - goto yy78; + goto yy91; default: - goto yy17; + goto yy21; } - yy48: + yy57: yych = *++cursor; switch (yych) { @@ -802,12 +894,12 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy49; + goto yy58; } - yy49: -#line 67 "lexer.re" + yy58: +#line 77 "lexer.re" { ts.push_token( Token(Token::Integer, lineno, token_start + 1, cursor)); @@ -815,8 +907,8 @@ bool Lexer::tokenize(TokenStream& ts) continue; } -#line 700 "lexer.cpp" - yy50: +#line 774 "lexer.cpp" + yy59: ++cursor; if (limit <= cursor) return true; @@ -833,7 +925,7 @@ bool Lexer::tokenize(TokenStream& ts) case '7': case '8': case '9': - goto yy50; + goto yy59; case 'A': case 'B': case 'C': @@ -887,31 +979,31 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy49; + goto yy58; } - yy52: + yy61: yych = *++cursor; switch (yych) { case 'a': - goto yy79; + goto yy92; case 't': - goto yy80; + goto yy93; default: - goto yy17; + goto yy21; } - yy53: + yy62: yych = *++cursor; switch (yych) { case 'b': - goto yy82; + goto yy95; default: - goto yy17; + goto yy21; } - yy54: + yy63: ++cursor; if (limit <= cursor) return true; @@ -940,20 +1032,53 @@ bool Lexer::tokenize(TokenStream& ts) case 'd': case 'e': case 'f': - goto yy54; + goto yy63; default: - goto yy56; + goto yy65; } - yy56: -#line 83 "lexer.re" + yy65: +#line 93 "lexer.re" { ts.push_token(Token(Token::XInteger, lineno, token_start, cursor)); token_start = cursor; continue; } -#line 822 "lexer.cpp" - yy57: +#line 896 "lexer.cpp" + yy66: + ++cursor; + yy67: +#line 68 "lexer.re" + { + ts.push_token( + Token(Token::FloatingPoint, lineno, token_start, cursor)); + token_start = cursor; + + continue; + } +#line 909 "lexer.cpp" + yy68: + ++cursor; + if (limit <= cursor) + return true; + yych = *cursor; + switch (yych) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + goto yy68; + default: + goto yy67; + } + yy70: yych = *++cursor; switch (yych) { @@ -1020,38 +1145,38 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy58; + goto yy71; } - yy58: -#line 137 "lexer.re" + yy71: +#line 147 "lexer.re" { ts.push_token(Token(Token::Add, lineno)); token_start = cursor; continue; } -#line 899 "lexer.cpp" - yy59: +#line 1003 "lexer.cpp" + yy72: yych = *++cursor; switch (yych) { case 'l': - goto yy84; + goto yy97; default: - goto yy17; + goto yy21; } - yy60: + yy73: yych = *++cursor; switch (yych) { case 's': - goto yy86; + goto yy99; default: - goto yy17; + goto yy21; } - yy61: + yy74: yych = *++cursor; switch (yych) { @@ -1118,20 +1243,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy62; + goto yy75; } - yy62: -#line 158 "lexer.re" + yy75: +#line 168 "lexer.re" { ts.push_token(Token(Token::Div, lineno)); token_start = cursor; continue; } -#line 988 "lexer.cpp" - yy63: +#line 1092 "lexer.cpp" + yy76: yych = *++cursor; switch (yych) { @@ -1198,20 +1323,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy64; + goto yy77; } - yy64: -#line 116 "lexer.re" + yy77: +#line 126 "lexer.re" { ts.push_token(Token(Token::Dup, lineno)); token_start = cursor; continue; } -#line 1065 "lexer.cpp" - yy65: +#line 1169 "lexer.cpp" + yy78: yych = *++cursor; switch (yych) { @@ -1276,33 +1401,33 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; case 'f': - goto yy87; + goto yy100; case 't': - goto yy89; + goto yy102; default: - goto yy66; + goto yy79; } - yy66: -#line 186 "lexer.re" + yy79: +#line 196 "lexer.re" { ts.push_token(Token(Token::Jump, lineno)); token_start = cursor; continue; } -#line 1142 "lexer.cpp" - yy67: +#line 1246 "lexer.cpp" + yy80: yych = *++cursor; switch (yych) { case 'e': - goto yy91; + goto yy104; default: - goto yy17; + goto yy21; } - yy68: + yy81: yych = *++cursor; switch (yych) { @@ -1369,29 +1494,29 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy69; + goto yy82; } - yy69: -#line 213 "lexer.re" + yy82: +#line 223 "lexer.re" { ts.push_token(Token(Token::LessEq, lineno)); token_start = cursor; continue; } -#line 1225 "lexer.cpp" - yy70: +#line 1329 "lexer.cpp" + yy83: yych = *++cursor; switch (yych) { case 's': - goto yy92; + goto yy105; default: - goto yy17; + goto yy21; } - yy71: + yy84: yych = *++cursor; switch (yych) { @@ -1458,20 +1583,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy72; + goto yy85; } - yy72: -#line 165 "lexer.re" + yy85: +#line 175 "lexer.re" { ts.push_token(Token(Token::Mod, lineno)); token_start = cursor; continue; } -#line 1308 "lexer.cpp" - yy73: +#line 1412 "lexer.cpp" + yy86: yych = *++cursor; switch (yych) { @@ -1538,20 +1663,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy74; + goto yy87; } - yy74: -#line 151 "lexer.re" + yy87: +#line 161 "lexer.re" { ts.push_token(Token(Token::Mul, lineno)); token_start = cursor; continue; } -#line 1385 "lexer.cpp" - yy75: +#line 1489 "lexer.cpp" + yy88: yych = *++cursor; switch (yych) { @@ -1617,49 +1742,49 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; case 'r': - goto yy94; + goto yy107; default: - goto yy76; + goto yy89; } - yy76: -#line 103 "lexer.re" + yy89: +#line 113 "lexer.re" { ts.push_token(Token(Token::Pop, lineno)); token_start = cursor; continue; } -#line 1462 "lexer.cpp" - yy77: +#line 1566 "lexer.cpp" + yy90: yych = *++cursor; switch (yych) { case 'n': - goto yy96; + goto yy109; default: - goto yy17; + goto yy21; } - yy78: + yy91: yych = *++cursor; switch (yych) { case 'h': - goto yy97; + goto yy110; default: - goto yy17; + goto yy21; } - yy79: + yy92: yych = *++cursor; switch (yych) { case 'd': - goto yy99; + goto yy112; default: - goto yy17; + goto yy21; } - yy80: + yy93: yych = *++cursor; switch (yych) { @@ -1726,20 +1851,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy81; + goto yy94; } - yy81: -#line 206 "lexer.re" + yy94: +#line 216 "lexer.re" { ts.push_token(Token(Token::Ret, lineno)); token_start = cursor; continue; } -#line 1557 "lexer.cpp" - yy82: +#line 1661 "lexer.cpp" + yy95: yych = *++cursor; switch (yych) { @@ -1806,20 +1931,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy83; + goto yy96; } - yy83: -#line 144 "lexer.re" + yy96: +#line 154 "lexer.re" { ts.push_token(Token(Token::Sub, lineno)); token_start = cursor; continue; } -#line 1634 "lexer.cpp" - yy84: +#line 1738 "lexer.cpp" + yy97: yych = *++cursor; switch (yych) { @@ -1886,29 +2011,29 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy85; + goto yy98; } - yy85: -#line 199 "lexer.re" + yy98: +#line 209 "lexer.re" { ts.push_token(Token(Token::Call, lineno)); token_start = cursor; continue; } -#line 1711 "lexer.cpp" - yy86: +#line 1815 "lexer.cpp" + yy99: yych = *++cursor; switch (yych) { case 't': - goto yy101; + goto yy114; default: - goto yy17; + goto yy21; } - yy87: + yy100: yych = *++cursor; switch (yych) { @@ -1975,20 +2100,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy88; + goto yy101; } - yy88: -#line 179 "lexer.re" + yy101: +#line 189 "lexer.re" { ts.push_token(Token(Token::JumpF, lineno)); token_start = cursor; continue; } -#line 1794 "lexer.cpp" - yy89: +#line 1898 "lexer.cpp" + yy102: yych = *++cursor; switch (yych) { @@ -2055,29 +2180,29 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy90; + goto yy103; } - yy90: -#line 172 "lexer.re" + yy103: +#line 182 "lexer.re" { ts.push_token(Token(Token::JumpT, lineno)); token_start = cursor; continue; } -#line 1871 "lexer.cpp" - yy91: +#line 1975 "lexer.cpp" + yy104: yych = *++cursor; switch (yych) { case 'l': - goto yy103; + goto yy116; default: - goto yy17; + goto yy21; } - yy92: + yy105: yych = *++cursor; switch (yych) { @@ -2144,20 +2269,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy93; + goto yy106; } - yy93: -#line 219 "lexer.re" + yy106: +#line 229 "lexer.re" { ts.push_token(Token(Token::Less, lineno)); token_start = cursor; continue; } -#line 1954 "lexer.cpp" - yy94: +#line 2058 "lexer.cpp" + yy107: yych = *++cursor; switch (yych) { @@ -2224,29 +2349,29 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy95; + goto yy108; } - yy95: -#line 109 "lexer.re" + yy108: +#line 119 "lexer.re" { ts.push_token(Token(Token::PopTo, lineno)); token_start = cursor; continue; } -#line 2031 "lexer.cpp" - yy96: +#line 2135 "lexer.cpp" + yy109: yych = *++cursor; switch (yych) { case 't': - goto yy105; + goto yy118; default: - goto yy17; + goto yy21; } - yy97: + yy110: yych = *++cursor; switch (yych) { @@ -2312,22 +2437,22 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; case 'r': - goto yy107; + goto yy120; default: - goto yy98; + goto yy111; } - yy98: -#line 97 "lexer.re" + yy111: +#line 107 "lexer.re" { ts.push_token(Token(Token::Push, lineno)); token_start = cursor; continue; } -#line 2114 "lexer.cpp" - yy99: +#line 2218 "lexer.cpp" + yy112: yych = *++cursor; switch (yych) { @@ -2394,20 +2519,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy100; + goto yy113; } - yy100: -#line 123 "lexer.re" + yy113: +#line 133 "lexer.re" { ts.push_token(Token(Token::Read, lineno)); token_start = cursor; continue; } -#line 2191 "lexer.cpp" - yy101: +#line 2295 "lexer.cpp" + yy114: yych = *++cursor; switch (yych) { @@ -2474,20 +2599,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy102; + goto yy115; } - yy102: -#line 192 "lexer.re" + yy115: +#line 202 "lexer.re" { ts.push_token(Token(Token::Const, lineno)); token_start = cursor; continue; } -#line 2268 "lexer.cpp" - yy103: +#line 2372 "lexer.cpp" + yy116: yych = *++cursor; switch (yych) { @@ -2554,20 +2679,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy104; + goto yy117; } - yy104: -#line 226 "lexer.re" + yy117: +#line 236 "lexer.re" { ts.push_token(Token(Token::Label, lineno)); token_start = cursor; continue; } -#line 2345 "lexer.cpp" - yy105: +#line 2449 "lexer.cpp" + yy118: yych = *++cursor; switch (yych) { @@ -2634,20 +2759,20 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy106; + goto yy119; } - yy106: -#line 130 "lexer.re" + yy119: +#line 140 "lexer.re" { ts.push_token(Token(Token::Print, lineno)); token_start = cursor; continue; } -#line 2422 "lexer.cpp" - yy107: +#line 2526 "lexer.cpp" + yy120: yych = *++cursor; switch (yych) { @@ -2714,21 +2839,21 @@ bool Lexer::tokenize(TokenStream& ts) case 'x': case 'y': case 'z': - goto yy16; + goto yy20; default: - goto yy108; + goto yy121; } - yy108: -#line 91 "lexer.re" + yy121: +#line 101 "lexer.re" { ts.push_token(Token(Token::PushFrom, lineno)); token_start = cursor; continue; } -#line 2499 "lexer.cpp" +#line 2603 "lexer.cpp" } -#line 264 "lexer.re" +#line 284 "lexer.re" } return true; } @@ -2738,7 +2863,7 @@ bool Lexer::tokenize(TokenStream& ts) ** 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 +** \param nbytes - the number of bytes */ void Lexer::buffer_grow(size_t new_size) diff --git a/spasm/src/asm/lexer.re b/spasm/src/asm/lexer.re index c182653..fabeaf7 100644 --- a/spasm/src/asm/lexer.re +++ b/spasm/src/asm/lexer.re @@ -13,16 +13,16 @@ namespace Lexer { Lexer::Lexer (std::istream & _file, size_t _buffer_size) - : file (&_file), buffer_size (_buffer_size), state (unsigned(-1)), - lineno (0) + : file (&_file), buffer_size (_buffer_size), state (unsigned(-1)), + lineno (0) { - buffer = new char[buffer_size]; - cursor = limit = marker = token_start = NULL; + buffer = new char[buffer_size]; + cursor = limit = marker = token_start = NULL; } Lexer::~Lexer () { - delete [] buffer; + delete [] buffer; } bool Lexer::tokenize (TokenStream &ts) @@ -44,285 +44,305 @@ re2c:define:YYSETSTATE:naked=1; /*!getstate:re2c */ - while (1) { + while (1) { /*!re2c -DIGIT = [0-9] ; -XDIGIT = [0-9a-fA-F] ; +DIGIT = [0-9] ; +XDIGIT = [0-9a-fA-F] ; -INTEGER = "-"? "0" | ([1-9] DIGIT*) ; -NONNEGATIVE = "0" | ([1-9] DIGIT*) ; -XINTEGER = "0" [xX] XDIGIT+ ; -IDENTIFIER = [a-zA-Z_] [0-9a-zA-Z_]* ; +INTEGER = "-"? "0" | ([1-9] DIGIT*) ; +NONNEGATIVE = "0" | ([1-9] DIGIT*) ; +XINTEGER = "0" [xX] XDIGIT+ ; +FLOATING = "-"? "0" | ([1-9] DIGIT*) "." NONNEGATIVE ; +IDENTIFIER = [a-zA-Z_] [0-9a-zA-Z_]* ; -INTEGER { - ts.push_token (Token (Token::Integer, lineno, - token_start, cursor)); - token_start = cursor; +INTEGER { + ts.push_token (Token (Token::Integer, lineno, + token_start, cursor)); + token_start = cursor; - continue; - } + continue; + } -"r" NONNEGATIVE { - ts.push_token (Token (Token::Integer, lineno, - token_start + 1, cursor)); - token_start = cursor; +FLOATING { + ts.push_token (Token (Token::FloatingPoint, + lineno, token_start, + cursor)); + token_start = cursor; - continue; - } -"a" NONNEGATIVE { - Token t(Token::Integer, lineno, - token_start + 1, cursor); - t.set_int(-t.value_int()); - ts.push_token(t); - token_start = cursor; - continue; - } + continue; + } -XINTEGER { - ts.push_token (Token (Token::XInteger, lineno, - token_start, cursor)); - token_start = cursor; +"r" NONNEGATIVE { + ts.push_token (Token (Token::Integer, lineno, + token_start + 1, cursor)); + token_start = cursor; - continue; - } + continue; + } +"a" NONNEGATIVE { + Token t(Token::Integer, lineno, + token_start + 1, cursor); + t.set_int(-t.value_int()); + ts.push_token(t); + token_start = cursor; + continue; + } -"pushr" { - ts.push_token (Token (Token::PushFrom, lineno)); - token_start = cursor; +XINTEGER { + ts.push_token (Token (Token::XInteger, lineno, + token_start, cursor)); + token_start = cursor; - continue; - } -"push" { - ts.push_token (Token (Token::Push, lineno)); - token_start = cursor; + continue; + } - continue; - } -"pop" { - ts.push_token (Token (Token::Pop, lineno)); - token_start = cursor; +"pushr" { + ts.push_token (Token (Token::PushFrom, lineno)); + token_start = cursor; - continue; - } -"popr" { - ts.push_token (Token (Token::PopTo, lineno)); - token_start = cursor; + continue; + } +"push" { + ts.push_token (Token (Token::Push, lineno)); + token_start = cursor; - continue; - } + continue; + } +"pop" { + ts.push_token (Token (Token::Pop, lineno)); + token_start = cursor; -"dup" { - ts.push_token (Token (Token::Dup, lineno)); - token_start = cursor; + continue; + } +"popr" { + ts.push_token (Token (Token::PopTo, lineno)); + token_start = cursor; - continue; - } + continue; + } -"read" { - ts.push_token (Token (Token::Read, lineno)); - token_start = cursor; +"dup" { + ts.push_token (Token (Token::Dup, lineno)); + token_start = cursor; - continue; - } + continue; + } -"print" { - ts.push_token (Token (Token::Print, lineno)); - token_start = cursor; +"read" { + ts.push_token (Token (Token::Read, lineno)); + token_start = cursor; - continue; - } + continue; + } -"add" { - ts.push_token (Token (Token::Add, lineno)); - token_start = cursor; +"print" { + ts.push_token (Token (Token::Print, lineno)); + token_start = cursor; - continue; - } + continue; + } -"sub" { - ts.push_token (Token (Token::Sub, lineno)); - token_start = cursor; +"add" { + ts.push_token (Token (Token::Add, lineno)); + token_start = cursor; - continue; - } + continue; + } -"mul" { - ts.push_token (Token (Token::Mul, lineno)); - token_start = cursor; +"sub" { + ts.push_token (Token (Token::Sub, lineno)); + token_start = cursor; - continue; - } + continue; + } -"div" { - ts.push_token (Token (Token::Div, lineno)); - token_start = cursor; +"mul" { + ts.push_token (Token (Token::Mul, lineno)); + token_start = cursor; - continue; - } + continue; + } -"mod" { - ts.push_token (Token (Token::Mod, lineno)); - token_start = cursor; +"div" { + ts.push_token (Token (Token::Div, lineno)); + token_start = cursor; - continue; - } + continue; + } -"jmpt" { - ts.push_token (Token (Token::JumpT, lineno)); - token_start = cursor; +"mod" { + ts.push_token (Token (Token::Mod, lineno)); + token_start = cursor; - continue; - } + continue; + } -"jmpf" { - ts.push_token (Token (Token::JumpF, lineno)); - token_start = cursor; +"jmpt" { + ts.push_token (Token (Token::JumpT, lineno)); + token_start = cursor; - continue; - } + continue; + } -"jmp" { - ts.push_token (Token (Token::Jump, lineno)); - token_start = cursor; +"jmpf" { + ts.push_token (Token (Token::JumpF, lineno)); + token_start = cursor; - continue; - } -"const" { - ts.push_token (Token (Token::Const, lineno)); - token_start = cursor; + continue; + } - continue; - } +"jmp" { + ts.push_token (Token (Token::Jump, lineno)); + token_start = cursor; -"call" { - ts.push_token (Token (Token::Call, lineno)); - token_start = cursor; + continue; + } +"const" { + ts.push_token (Token (Token::Const, lineno)); + token_start = cursor; - continue; - } + continue; + } -"ret" { - ts.push_token (Token (Token::Ret, lineno)); - token_start = cursor; +"call" { + ts.push_token (Token (Token::Call, lineno)); + token_start = cursor; - continue; - } + continue; + } -"leq" { - ts.push_token (Token (Token::LessEq, lineno)); - token_start = cursor; +"ret" { + ts.push_token (Token (Token::Ret, lineno)); + token_start = cursor; - continue; - } -"less" { - ts.push_token (Token (Token::Less, lineno)); - token_start = cursor; + continue; + } - continue; - } +"leq" { + ts.push_token (Token (Token::LessEq, lineno)); + token_start = cursor; -"label" { - ts.push_token (Token (Token::Label, lineno)); - token_start = cursor; + continue; + } +"less" { + ts.push_token (Token (Token::Less, lineno)); + token_start = cursor; - continue; - } + continue; + } -IDENTIFIER { - ts.push_token (Token (Token::Ident, lineno, token_start, - cursor)); - token_start = cursor; +"label" { + ts.push_token (Token (Token::Label, lineno)); + token_start = cursor; - continue; - } + continue; + } -[ ]+ { - token_start = cursor; +IDENTIFIER { + ts.push_token (Token (Token::Ident, lineno, token_start, + cursor)); + token_start = cursor; - continue; - } + continue; + } -"\n" { - ++lineno; - token_start = cursor; +[ ]+ { + token_start = cursor; - continue; - } + continue; + } +DIGIT* ":" { + token_start = cursor; -"\000" { - ts.push_token (Token (Token::EndInput, lineno)); + continue; + } +"#" .* "\n" { + ++lineno; + token_start = cursor; - break; - } + continue; + } +"\n" { + ++lineno; + token_start = cursor; -[^] { - return false; - } + continue; + } + +"\000" { + ts.push_token (Token (Token::EndInput, lineno)); + + break; + } + +[^] { + return false; + } */ - } - return true; - } + } + 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 +** \param nbytes - the number of bytes */ void Lexer::buffer_grow (size_t new_size) { - char *new_buffer = new char[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); - } + 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'); - } + 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 SpasmImpl +} // namespace ASM +} // namespace SpasmImpl diff --git a/spasm/src/asm/token.cpp b/spasm/src/asm/token.cpp index 4f298cf..fa612e3 100644 --- a/spasm/src/asm/token.cpp +++ b/spasm/src/asm/token.cpp @@ -22,20 +22,33 @@ Token::Token(Token::Token_type type, const char* end) : _type(type), _lineno(lineno) { - if (_type == Integer || _type == XInteger) + switch (_type) { - _value_int = strtol(start, NULL, 0); - } - else if (_type == Ident) - { - if (end) + case Integer: + case XInteger: + { + _value_int = strtol(start, NULL, 0); + break; + } + case FloatingPoint: { - _value_str = std::string(start, end); + _value_double = strtod(start, NULL); + break; } - else + case Ident: { - _value_str = std::string(start); + if (end) + { + _value_str = std::string(start, end); + } + else + { + _value_str = std::string(start); + } + break; } + default: + break; } } @@ -44,7 +57,7 @@ Token::Token_type Token::type() const return _type; } -int Token::value_int() const +int64_t Token::value_int() const { return _value_int; } diff --git a/spasm/src/asm/token.hpp b/spasm/src/asm/token.hpp index 364b315..8f18768 100644 --- a/spasm/src/asm/token.hpp +++ b/spasm/src/asm/token.hpp @@ -45,6 +45,7 @@ class Token // Register, Integer, XInteger, + FloatingPoint, EndInput, NotUsed }; @@ -57,8 +58,10 @@ class Token size_t lineno() const; - int value_int() const; - void set_int(int v) { _value_int = v; } + int64_t value_int() const; + void set_int(int64_t v) { _value_int = v; } + + double value_double() const { return _value_double; } const std::string& value_str() const; @@ -67,7 +70,8 @@ class Token size_t _lineno; - int _value_int; + int64_t _value_int; + double _value_double; std::string _value_str; }; |
