Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitd7b2e339ff06c64bcb1322eec396d4995da6b17a (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-15 10:11:01 +0200
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-15 10:16:21 +0200
Filesace7bdc0df3791b6db8878d52ba98cb368e180b8
IPL-d7b2e339ff06c64bcb1322eec396d4995da6b17a.tar
IPL-d7b2e339ff06c64bcb1322eec396d4995da6b17a.tar.gz
IPL-d7b2e339ff06c64bcb1322eec396d4995da6b17a.zip
Parentfb7e4dbb3ec65de00987f2ccfc6d29ebfcec12c7 (diff)
Implemented push and pop with numbers
8 files changed, 671 insertions, 381 deletions
-rw-r--r--JSImpl/test/sprtTests.cpp49
-rw-r--r--spasm/src/asm/bytecode.hpp2
-rw-r--r--spasm/src/asm/lexer.cpp808
-rw-r--r--spasm/src/asm/lexer.re27
-rw-r--r--spasm/src/asm/token.hpp93
-rw-r--r--spasm/src/spasm.cpp28
-rw-r--r--spasm/src/spasm.hpp28
-rw-r--r--spasm/src/spasm_impl.hpp17
diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp
index fe743f9..eef7147 100644
--- a/JSImpl/test/sprtTests.cpp
+++ b/JSImpl/test/sprtTests.cpp
@@ -198,22 +198,22 @@ TEST_F(SPRTTest, JumpFalse)
TEST_F(SPRTTest, Call)
{
Spasm::byte bytecode[] = {
- OpCodes::Alloc, 5, // 2
+ OpCodes::Push, 5, // 2
OpCodes::Const, 1, 0, // 5
OpCodes::Const, 2, 6, // 8
OpCodes::Const, 3, 7, // 11
OpCodes::Const, 4, 2, // 14
- OpCodes::Push, 3, // 16
- OpCodes::Push, 2, // 18
- OpCodes::Push, 4, // 20
+ OpCodes::PushFrom, 3, // 16
+ OpCodes::PushFrom, 2, // 18
+ OpCodes::PushFrom, 4, // 20
OpCodes::Call, 25, // 22
OpCodes::Print, 4, // 24
- OpCodes::Halt, // 25
+ OpCodes::Halt, // 25
OpCodes::Print, 0, // 27
OpCodes::Print, -1, // 29
OpCodes::Print, -2, // 31
OpCodes::Mul, 1, -2, -1,// 33
- OpCodes::Ret, 1, // 35
+ OpCodes::Ret, 1, // 35
};
Run(bytecode, sizeof(bytecode));
@@ -232,17 +232,44 @@ TEST_F(SPRTTest, Read)
ASSERT_EQ(Output.str(), "42");
}
+TEST_F(SPASMTest, RSyntax)
+{
+ const char* program =
+ "push 5" "\n"
+ "const r1 0" "\n"
+ "const r2 6" "\n"
+ "const r3 7" "\n"
+ "const r4 2" "\n"
+ "pushr r3" "\n"
+ "pushr r2" "\n"
+ "pushr r4" "\n"
+ "call mult" "\n"
+ "popr r1" "\n"
+ "print r1" "\n"
+ "halt" "\n"
+ "label mult" "\n"
+ "print a0" "\n"
+ "print a1" "\n"
+ "print a2" "\n"
+ "mul r1 a2 a1" "\n"
+ "ret r1" "\n"
+ ""
+ ;
+ CompileAndRun(program);
+ ASSERT_EQ(Output.str(), "26742");
+}
+
TEST_F(SPASMTest, Call)
{
const char* program =
- "alloc 5" "\n"
+ "push 5" "\n"
"const 1 0" "\n"
"const 2 6" "\n"
"const 3 7" "\n"
"const 4 2" "\n"
- "push 3" "\n"
- "push 2" "\n"
- "push 4" "\n"
+ "pushr 3" "\n"
+ "pushr 2" "\n"
+ "pushr 4" "\n"
"call mult" "\n"
"print 4" "\n"
"halt" "\n"
@@ -261,7 +288,7 @@ TEST_F(SPASMTest, Call)
TEST_F(SPASMTest, GCD)
{
const char* program =
- "alloc 3" "\n"
+ "push 3" "\n"
"read 1" "\n"
"read 2" "\n"
"label loop" "\n"
diff --git a/spasm/src/asm/bytecode.hpp b/spasm/src/asm/bytecode.hpp
index 9ae90c1..374ccef 100644
--- a/spasm/src/asm/bytecode.hpp
+++ b/spasm/src/asm/bytecode.hpp
@@ -14,7 +14,7 @@ class Bytecode_Stream
{
public:
typedef int8_t byte;
- typedef byte Opcode_t;
+ typedef byte Opcode_t;
virtual ~Bytecode_Stream();
virtual void push_opcode(Opcode_t) = 0;
diff --git a/spasm/src/asm/lexer.cpp b/spasm/src/asm/lexer.cpp
index a012650..3af83c7 100644
--- a/spasm/src/asm/lexer.cpp
+++ b/spasm/src/asm/lexer.cpp
@@ -1,4 +1,4 @@
-/* Generated by re2c 1.1.1 on Wed Nov 14 19:46:01 2018 */
+/* Generated by re2c 1.1.1 on Thu Nov 15 10:01:26 2018 */
#line 1 "lexer.re"
#include <iostream>
@@ -128,7 +128,7 @@ bool Lexer::tokenize(TokenStream& ts)
}
yy2:
++cursor;
-#line 237 "lexer.re"
+#line 254 "lexer.re"
{
ts.push_token(Token(Token::EndInput, lineno));
@@ -138,7 +138,7 @@ bool Lexer::tokenize(TokenStream& ts)
yy4:
++cursor;
yy5:
-#line 243 "lexer.re"
+#line 260 "lexer.re"
{
return false;
}
@@ -157,7 +157,7 @@ bool Lexer::tokenize(TokenStream& ts)
goto yy8;
}
yy8:
-#line 224 "lexer.re"
+#line 241 "lexer.re"
{
token_start = cursor;
@@ -166,10 +166,9 @@ bool Lexer::tokenize(TokenStream& ts)
#line 150 "lexer.cpp"
yy9:
++cursor;
-#line 230 "lexer.re"
+#line 247 "lexer.re"
{
++lineno;
-
token_start = cursor;
continue;
@@ -195,12 +194,9 @@ bool Lexer::tokenize(TokenStream& ts)
goto yy13;
}
yy13:
-#line 58 "lexer.re"
+#line 59 "lexer.re"
{
- ts.push_token(Token(Token::Integer, lineno,
-
- token_start, cursor));
-
+ ts.push_token(Token(Token::Integer, lineno, token_start, cursor));
token_start = cursor;
continue;
@@ -303,12 +299,9 @@ bool Lexer::tokenize(TokenStream& ts)
goto yy18;
}
yy18:
-#line 216 "lexer.re"
+#line 233 "lexer.re"
{
- ts.push_token(Token(Token::Ident, lineno, token_start,
-
- cursor));
-
+ ts.push_token(Token(Token::Ident, lineno, token_start, cursor));
token_start = cursor;
continue;
@@ -318,10 +311,20 @@ bool Lexer::tokenize(TokenStream& ts)
yych = *++cursor;
switch (yych)
{
- case 'd':
+ case '0':
goto yy31;
- case 'l':
- goto yy32;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ goto yy33;
+ case 'd':
+ goto yy35;
default:
goto yy17;
}
@@ -330,9 +333,9 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'a':
- goto yy33;
+ goto yy36;
case 'o':
- goto yy34;
+ goto yy37;
default:
goto yy17;
}
@@ -341,9 +344,9 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'i':
- goto yy35;
+ goto yy38;
case 'u':
- goto yy36;
+ goto yy39;
default:
goto yy17;
}
@@ -352,7 +355,7 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'm':
- goto yy37;
+ goto yy40;
default:
goto yy17;
}
@@ -361,9 +364,9 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'a':
- goto yy38;
+ goto yy41;
case 'e':
- goto yy39;
+ goto yy42;
default:
goto yy17;
}
@@ -372,9 +375,9 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'o':
- goto yy40;
+ goto yy43;
case 'u':
- goto yy41;
+ goto yy44;
default:
goto yy17;
}
@@ -383,11 +386,11 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'o':
- goto yy42;
+ goto yy45;
case 'r':
- goto yy43;
+ goto yy46;
case 'u':
- goto yy44;
+ goto yy47;
default:
goto yy17;
}
@@ -395,8 +398,20 @@ bool Lexer::tokenize(TokenStream& ts)
yych = *++cursor;
switch (yych)
{
+ case '0':
+ goto yy48;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ goto yy50;
case 'e':
- goto yy45;
+ goto yy52;
default:
goto yy17;
}
@@ -405,7 +420,7 @@ bool Lexer::tokenize(TokenStream& ts)
switch (yych)
{
case 'u':
- goto yy46;
+ goto yy53;
default:
goto yy17;
}
@@ -438,7 +453,7 @@ bool Lexer::tokenize(TokenStream& ts)
case 'd':
case 'e':
case 'f':
- goto yy47;
+ goto yy54;
default:
goto yy30;
}
@@ -449,151 +464,454 @@ bool Lexer::tokenize(TokenStream& ts)
yych = *++cursor;
switch (yych)
{
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '_':
+ case 'a':
+ case 'b':
+ case 'c':
case 'd':
- goto yy50;
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ goto yy16;
default:
- goto yy17;
+ goto yy32;
}
yy32:
- yych = *++cursor;
+#line 74 "lexer.re"
+ {
+ Token t(Token::Integer, lineno, token_start + 1, cursor);
+ t.set_int(-t.value_int());
+ ts.push_token(t);
+ token_start = cursor;
+ continue;
+ }
+#line 473 "lexer.cpp"
+ yy33:
+ ++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 yy33;
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '_':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
case 'l':
- goto yy52;
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ goto yy16;
+ default:
+ goto yy32;
+ }
+ yy35:
+ yych = *++cursor;
+ switch (yych)
+ {
+ case 'd':
+ goto yy57;
default:
goto yy17;
}
- yy33:
+ yy36:
yych = *++cursor;
switch (yych)
{
case 'l':
- goto yy53;
+ goto yy59;
default:
goto yy17;
}
- yy34:
+ yy37:
yych = *++cursor;
switch (yych)
{
case 'n':
- goto yy54;
+ goto yy60;
default:
goto yy17;
}
- yy35:
+ yy38:
yych = *++cursor;
switch (yych)
{
case 'v':
- goto yy55;
+ goto yy61;
default:
goto yy17;
}
- yy36:
+ yy39:
yych = *++cursor;
switch (yych)
{
case 'p':
- goto yy57;
+ goto yy63;
default:
goto yy17;
}
- yy37:
+ yy40:
yych = *++cursor;
switch (yych)
{
case 'p':
- goto yy59;
+ goto yy65;
default:
goto yy17;
}
- yy38:
+ yy41:
yych = *++cursor;
switch (yych)
{
case 'b':
- goto yy61;
+ goto yy67;
default:
goto yy17;
}
- yy39:
+ yy42:
yych = *++cursor;
switch (yych)
{
case 'q':
- goto yy62;
+ goto yy68;
case 's':
- goto yy64;
+ goto yy70;
default:
goto yy17;
}
- yy40:
+ yy43:
yych = *++cursor;
switch (yych)
{
case 'd':
- goto yy65;
+ goto yy71;
default:
goto yy17;
}
- yy41:
+ yy44:
yych = *++cursor;
switch (yych)
{
case 'l':
- goto yy67;
+ goto yy73;
default:
goto yy17;
}
- yy42:
+ yy45:
yych = *++cursor;
switch (yych)
{
case 'p':
- goto yy69;
+ goto yy75;
default:
goto yy17;
}
- yy43:
+ yy46:
yych = *++cursor;
switch (yych)
{
case 'i':
- goto yy71;
+ goto yy77;
default:
goto yy17;
}
- yy44:
+ yy47:
yych = *++cursor;
switch (yych)
{
case 's':
- goto yy72;
+ goto yy78;
default:
goto yy17;
}
- yy45:
+ yy48:
yych = *++cursor;
switch (yych)
{
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '_':
case 'a':
- goto yy73;
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
case 't':
- goto yy74;
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ goto yy16;
+ default:
+ goto yy49;
+ }
+ yy49:
+#line 67 "lexer.re"
+ {
+ ts.push_token(
+ Token(Token::Integer, lineno, token_start + 1, cursor));
+ token_start = cursor;
+
+ continue;
+ }
+#line 700 "lexer.cpp"
+ yy50:
+ ++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 yy50;
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '_':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ goto yy16;
+ default:
+ goto yy49;
+ }
+ yy52:
+ yych = *++cursor;
+ switch (yych)
+ {
+ case 'a':
+ goto yy79;
+ case 't':
+ goto yy80;
default:
goto yy17;
}
- yy46:
+ yy53:
yych = *++cursor;
switch (yych)
{
case 'b':
- goto yy76;
+ goto yy82;
default:
goto yy17;
}
- yy47:
+ yy54:
++cursor;
if (limit <= cursor)
return true;
@@ -622,23 +940,20 @@ bool Lexer::tokenize(TokenStream& ts)
case 'd':
case 'e':
case 'f':
- goto yy47;
+ goto yy54;
default:
- goto yy49;
+ goto yy56;
}
- yy49:
-#line 66 "lexer.re"
+ yy56:
+#line 83 "lexer.re"
{
- ts.push_token(Token(Token::XInteger, lineno,
-
- token_start, cursor));
-
+ ts.push_token(Token(Token::XInteger, lineno, token_start, cursor));
token_start = cursor;
continue;
}
-#line 512 "lexer.cpp"
- yy50:
+#line 822 "lexer.cpp"
+ yy57:
yych = *++cursor;
switch (yych)
{
@@ -707,46 +1022,36 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy51;
+ goto yy58;
}
- yy51:
-#line 120 "lexer.re"
+ yy58:
+#line 137 "lexer.re"
{
ts.push_token(Token(Token::Add, lineno));
-
token_start = cursor;
continue;
}
-#line 589 "lexer.cpp"
- yy52:
- yych = *++cursor;
- switch (yych)
- {
- case 'o':
- goto yy78;
- default:
- goto yy17;
- }
- yy53:
+#line 899 "lexer.cpp"
+ yy59:
yych = *++cursor;
switch (yych)
{
case 'l':
- goto yy79;
+ goto yy84;
default:
goto yy17;
}
- yy54:
+ yy60:
yych = *++cursor;
switch (yych)
{
case 's':
- goto yy81;
+ goto yy86;
default:
goto yy17;
}
- yy55:
+ yy61:
yych = *++cursor;
switch (yych)
{
@@ -815,19 +1120,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy56;
+ goto yy62;
}
- yy56:
-#line 141 "lexer.re"
+ yy62:
+#line 158 "lexer.re"
{
ts.push_token(Token(Token::Div, lineno));
-
token_start = cursor;
continue;
}
-#line 684 "lexer.cpp"
- yy57:
+#line 988 "lexer.cpp"
+ yy63:
yych = *++cursor;
switch (yych)
{
@@ -896,19 +1200,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy58;
+ goto yy64;
}
- yy58:
-#line 99 "lexer.re"
+ yy64:
+#line 116 "lexer.re"
{
ts.push_token(Token(Token::Dup, lineno));
-
token_start = cursor;
continue;
}
-#line 761 "lexer.cpp"
- yy59:
+#line 1065 "lexer.cpp"
+ yy65:
yych = *++cursor;
switch (yych)
{
@@ -975,32 +1278,31 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
case 'f':
- goto yy82;
+ goto yy87;
case 't':
- goto yy84;
+ goto yy89;
default:
- goto yy60;
+ goto yy66;
}
- yy60:
-#line 169 "lexer.re"
+ yy66:
+#line 186 "lexer.re"
{
ts.push_token(Token(Token::Jump, lineno));
-
token_start = cursor;
continue;
}
-#line 838 "lexer.cpp"
- yy61:
+#line 1142 "lexer.cpp"
+ yy67:
yych = *++cursor;
switch (yych)
{
case 'e':
- goto yy86;
+ goto yy91;
default:
goto yy17;
}
- yy62:
+ yy68:
yych = *++cursor;
switch (yych)
{
@@ -1069,28 +1371,27 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy63;
+ goto yy69;
}
- yy63:
-#line 196 "lexer.re"
+ yy69:
+#line 213 "lexer.re"
{
ts.push_token(Token(Token::LessEq, lineno));
-
token_start = cursor;
continue;
}
-#line 921 "lexer.cpp"
- yy64:
+#line 1225 "lexer.cpp"
+ yy70:
yych = *++cursor;
switch (yych)
{
case 's':
- goto yy87;
+ goto yy92;
default:
goto yy17;
}
- yy65:
+ yy71:
yych = *++cursor;
switch (yych)
{
@@ -1159,19 +1460,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy66;
+ goto yy72;
}
- yy66:
-#line 148 "lexer.re"
+ yy72:
+#line 165 "lexer.re"
{
ts.push_token(Token(Token::Mod, lineno));
-
token_start = cursor;
continue;
}
-#line 1004 "lexer.cpp"
- yy67:
+#line 1308 "lexer.cpp"
+ yy73:
yych = *++cursor;
switch (yych)
{
@@ -1240,19 +1540,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy68;
+ goto yy74;
}
- yy68:
-#line 134 "lexer.re"
+ yy74:
+#line 151 "lexer.re"
{
ts.push_token(Token(Token::Mul, lineno));
-
token_start = cursor;
continue;
}
-#line 1081 "lexer.cpp"
- yy69:
+#line 1385 "lexer.cpp"
+ yy75:
yych = *++cursor;
switch (yych)
{
@@ -1310,8 +1609,8 @@ bool Lexer::tokenize(TokenStream& ts)
case 'o':
case 'p':
case 'q':
- case 'r':
case 's':
+ case 't':
case 'u':
case 'v':
case 'w':
@@ -1319,49 +1618,48 @@ bool Lexer::tokenize(TokenStream& ts)
case 'y':
case 'z':
goto yy16;
- case 't':
- goto yy89;
+ case 'r':
+ goto yy94;
default:
- goto yy70;
+ goto yy76;
}
- yy70:
-#line 86 "lexer.re"
+ yy76:
+#line 103 "lexer.re"
{
ts.push_token(Token(Token::Pop, lineno));
-
token_start = cursor;
continue;
}
-#line 1158 "lexer.cpp"
- yy71:
+#line 1462 "lexer.cpp"
+ yy77:
yych = *++cursor;
switch (yych)
{
case 'n':
- goto yy90;
+ goto yy96;
default:
goto yy17;
}
- yy72:
+ yy78:
yych = *++cursor;
switch (yych)
{
case 'h':
- goto yy91;
+ goto yy97;
default:
goto yy17;
}
- yy73:
+ yy79:
yych = *++cursor;
switch (yych)
{
case 'd':
- goto yy93;
+ goto yy99;
default:
goto yy17;
}
- yy74:
+ yy80:
yych = *++cursor;
switch (yych)
{
@@ -1430,19 +1728,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy75;
+ goto yy81;
}
- yy75:
-#line 189 "lexer.re"
+ yy81:
+#line 206 "lexer.re"
{
ts.push_token(Token(Token::Ret, lineno));
-
token_start = cursor;
continue;
}
-#line 1253 "lexer.cpp"
- yy76:
+#line 1557 "lexer.cpp"
+ yy82:
yych = *++cursor;
switch (yych)
{
@@ -1511,28 +1808,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy77;
+ goto yy83;
}
- yy77:
-#line 127 "lexer.re"
+ yy83:
+#line 144 "lexer.re"
{
ts.push_token(Token(Token::Sub, lineno));
-
token_start = cursor;
continue;
}
-#line 1330 "lexer.cpp"
- yy78:
- yych = *++cursor;
- switch (yych)
- {
- case 'c':
- goto yy95;
- default:
- goto yy17;
- }
- yy79:
+#line 1634 "lexer.cpp"
+ yy84:
yych = *++cursor;
switch (yych)
{
@@ -1601,28 +1888,27 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy80;
+ goto yy85;
}
- yy80:
-#line 182 "lexer.re"
+ yy85:
+#line 199 "lexer.re"
{
ts.push_token(Token(Token::Call, lineno));
-
token_start = cursor;
continue;
}
-#line 1413 "lexer.cpp"
- yy81:
+#line 1711 "lexer.cpp"
+ yy86:
yych = *++cursor;
switch (yych)
{
case 't':
- goto yy97;
+ goto yy101;
default:
goto yy17;
}
- yy82:
+ yy87:
yych = *++cursor;
switch (yych)
{
@@ -1691,19 +1977,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy83;
+ goto yy88;
}
- yy83:
-#line 162 "lexer.re"
+ yy88:
+#line 179 "lexer.re"
{
ts.push_token(Token(Token::JumpF, lineno));
-
token_start = cursor;
continue;
}
-#line 1496 "lexer.cpp"
- yy84:
+#line 1794 "lexer.cpp"
+ yy89:
yych = *++cursor;
switch (yych)
{
@@ -1772,28 +2057,27 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy85;
+ goto yy90;
}
- yy85:
-#line 155 "lexer.re"
+ yy90:
+#line 172 "lexer.re"
{
ts.push_token(Token(Token::JumpT, lineno));
-
token_start = cursor;
continue;
}
-#line 1573 "lexer.cpp"
- yy86:
+#line 1871 "lexer.cpp"
+ yy91:
yych = *++cursor;
switch (yych)
{
case 'l':
- goto yy99;
+ goto yy103;
default:
goto yy17;
}
- yy87:
+ yy92:
yych = *++cursor;
switch (yych)
{
@@ -1862,37 +2146,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy88;
+ goto yy93;
}
- yy88:
-#line 202 "lexer.re"
+ yy93:
+#line 219 "lexer.re"
{
ts.push_token(Token(Token::Less, lineno));
-
token_start = cursor;
continue;
}
-#line 1656 "lexer.cpp"
- yy89:
- yych = *++cursor;
- switch (yych)
- {
- case 'o':
- goto yy101;
- default:
- goto yy17;
- }
- yy90:
- yych = *++cursor;
- switch (yych)
- {
- case 't':
- goto yy103;
- default:
- goto yy17;
- }
- yy91:
+#line 1954 "lexer.cpp"
+ yy94:
yych = *++cursor;
switch (yych)
{
@@ -1961,19 +2226,27 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy92;
+ goto yy95;
}
- yy92:
-#line 74 "lexer.re"
+ yy95:
+#line 109 "lexer.re"
{
- ts.push_token(Token(Token::Push, lineno));
-
+ ts.push_token(Token(Token::PopTo, lineno));
token_start = cursor;
continue;
}
-#line 1745 "lexer.cpp"
- yy93:
+#line 2031 "lexer.cpp"
+ yy96:
+ yych = *++cursor;
+ switch (yych)
+ {
+ case 't':
+ goto yy105;
+ default:
+ goto yy17;
+ }
+ yy97:
yych = *++cursor;
switch (yych)
{
@@ -2031,7 +2304,6 @@ bool Lexer::tokenize(TokenStream& ts)
case 'o':
case 'p':
case 'q':
- case 'r':
case 's':
case 't':
case 'u':
@@ -2041,20 +2313,21 @@ bool Lexer::tokenize(TokenStream& ts)
case 'y':
case 'z':
goto yy16;
+ case 'r':
+ goto yy107;
default:
- goto yy94;
+ goto yy98;
}
- yy94:
-#line 106 "lexer.re"
+ yy98:
+#line 97 "lexer.re"
{
- ts.push_token(Token(Token::Read, lineno));
-
+ ts.push_token(Token(Token::Push, lineno));
token_start = cursor;
continue;
}
-#line 1822 "lexer.cpp"
- yy95:
+#line 2114 "lexer.cpp"
+ yy99:
yych = *++cursor;
switch (yych)
{
@@ -2123,19 +2396,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy96;
+ goto yy100;
}
- yy96:
-#line 80 "lexer.re"
+ yy100:
+#line 123 "lexer.re"
{
- ts.push_token(Token(Token::Alloc, lineno));
-
+ ts.push_token(Token(Token::Read, lineno));
token_start = cursor;
continue;
}
-#line 1899 "lexer.cpp"
- yy97:
+#line 2191 "lexer.cpp"
+ yy101:
yych = *++cursor;
switch (yych)
{
@@ -2204,19 +2476,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy98;
+ goto yy102;
}
- yy98:
-#line 175 "lexer.re"
+ yy102:
+#line 192 "lexer.re"
{
ts.push_token(Token(Token::Const, lineno));
-
token_start = cursor;
continue;
}
-#line 1976 "lexer.cpp"
- yy99:
+#line 2268 "lexer.cpp"
+ yy103:
yych = *++cursor;
switch (yych)
{
@@ -2285,19 +2556,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy100;
+ goto yy104;
}
- yy100:
-#line 209 "lexer.re"
+ yy104:
+#line 226 "lexer.re"
{
ts.push_token(Token(Token::Label, lineno));
-
token_start = cursor;
continue;
}
-#line 2053 "lexer.cpp"
- yy101:
+#line 2345 "lexer.cpp"
+ yy105:
yych = *++cursor;
switch (yych)
{
@@ -2366,19 +2636,18 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy102;
+ goto yy106;
}
- yy102:
-#line 92 "lexer.re"
+ yy106:
+#line 130 "lexer.re"
{
- ts.push_token(Token(Token::PopTo, lineno));
-
+ ts.push_token(Token(Token::Print, lineno));
token_start = cursor;
continue;
}
-#line 2130 "lexer.cpp"
- yy103:
+#line 2422 "lexer.cpp"
+ yy107:
yych = *++cursor;
switch (yych)
{
@@ -2447,20 +2716,19 @@ bool Lexer::tokenize(TokenStream& ts)
case 'z':
goto yy16;
default:
- goto yy104;
+ goto yy108;
}
- yy104:
-#line 113 "lexer.re"
+ yy108:
+#line 91 "lexer.re"
{
- ts.push_token(Token(Token::Print, lineno));
-
+ ts.push_token(Token(Token::PushFrom, lineno));
token_start = cursor;
continue;
}
-#line 2207 "lexer.cpp"
+#line 2499 "lexer.cpp"
}
-#line 247 "lexer.re"
+#line 264 "lexer.re"
}
return true;
}
diff --git a/spasm/src/asm/lexer.re b/spasm/src/asm/lexer.re
index 298e340..c182653 100644
--- a/spasm/src/asm/lexer.re
+++ b/spasm/src/asm/lexer.re
@@ -52,6 +52,7 @@ 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_]* ;
@@ -63,6 +64,22 @@ INTEGER {
continue;
}
+"r" NONNEGATIVE {
+ ts.push_token (Token (Token::Integer, lineno,
+ token_start + 1, 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;
+ }
+
XINTEGER {
ts.push_token (Token (Token::XInteger, lineno,
token_start, cursor));
@@ -71,14 +88,14 @@ XINTEGER {
continue;
}
-"push" {
- ts.push_token (Token (Token::Push, lineno));
+"pushr" {
+ ts.push_token (Token (Token::PushFrom, lineno));
token_start = cursor;
continue;
}
-"alloc" {
- ts.push_token (Token (Token::Alloc, lineno));
+"push" {
+ ts.push_token (Token (Token::Push, lineno));
token_start = cursor;
continue;
@@ -89,7 +106,7 @@ XINTEGER {
continue;
}
-"popto" {
+"popr" {
ts.push_token (Token (Token::PopTo, lineno));
token_start = cursor;
diff --git a/spasm/src/asm/token.hpp b/spasm/src/asm/token.hpp
index a763196..364b315 100644
--- a/spasm/src/asm/token.hpp
+++ b/spasm/src/asm/token.hpp
@@ -3,34 +3,6 @@
#include <string>
-#define TOKENS \
- TOK(Halt) \
- TOK(Dup) \
- TOK(Pop) \
- TOK(PopTo) \
- TOK(Push) \
- TOK(Alloc) \
- TOK(Print) \
- TOK(Read) \
- TOK(Call) \
- TOK(Ret) \
- TOK(Jump) \
- TOK(JumpT) \
- TOK(JumpF) \
- TOK(Const) \
- TOK(Add) \
- TOK(Sub) \
- TOK(Mul) \
- TOK(Div) \
- TOK(Mod) \
- TOK(Less) \
- TOK(LessEq) \
- TOK(Label) \
- TOK(Ident) \
- TOK(Integer) \
- TOK(XInteger) \
- TOK(EndInput)
-
namespace SpasmImpl
{
namespace ASM
@@ -42,38 +14,38 @@ class Token
public:
enum Token_type
{
- _NoArgBegin,
- Halt = _NoArgBegin,
- Dup,
- Pop,
- _OneArgBegin,
- PopTo = _OneArgBegin,
- Push,
- Alloc,
- Print,
- Read,
- Call,
- Ret,
- Jump,
- _TwoArgBegin,
- JumpT = _TwoArgBegin,
- JumpF,
- Const,
- _ThreeArgBegin,
- Add = _ThreeArgBegin,
- Sub,
- Mul,
- Div,
- Mod,
- Less,
- LessEq,
- _NotOpCodeBegin,
- Label = _NotOpCodeBegin,
- Ident,
- //Register,
- Integer,
- XInteger,
- EndInput,
+ _NoArgBegin,
+ Halt = _NoArgBegin,
+ Dup,
+ Pop,
+ _OneArgBegin,
+ PopTo = _OneArgBegin,
+ PushFrom,
+ Push,
+ Print,
+ Read,
+ Call,
+ Ret,
+ Jump,
+ _TwoArgBegin,
+ JumpT = _TwoArgBegin,
+ JumpF,
+ Const,
+ _ThreeArgBegin,
+ Add = _ThreeArgBegin,
+ Sub,
+ Mul,
+ Div,
+ Mod,
+ Less,
+ LessEq,
+ _NotOpCodeBegin,
+ Label = _NotOpCodeBegin,
+ Ident,
+ // Register,
+ Integer,
+ XInteger,
+ EndInput,
NotUsed
};
@@ -86,6 +58,7 @@ class Token
size_t lineno() const;
int value_int() const;
+ void set_int(int v) { _value_int = v; }
const std::string& value_str() const;
diff --git a/spasm/src/spasm.cpp b/spasm/src/spasm.cpp
index d06552d..8e5e36d 100644
--- a/spasm/src/spasm.cpp
+++ b/spasm/src/spasm.cpp
@@ -55,23 +55,30 @@ Spasm::RunResult Spasm::run()
dup();
break;
case OpCodes::Pop:
- pop();
+ {
+ const auto count = PC_t(read_reg(size));
+ assert(m_SP - count >= m_FP);
+ m_SP -= count;
break;
+ }
case OpCodes::PopTo:
+ {
+ const auto arg0 = read_reg(size);
+ popto(arg0);
break;
- case OpCodes::Push:
+ }
+ case OpCodes::PushFrom:
{
const auto arg0 = read_reg(size);
push(arg0);
break;
}
- case OpCodes::Alloc:
+ case OpCodes::Push:
{
const auto count = PC_t(read_reg(size));
- for (PC_t i = 0; i < count; ++i)
- {
- push_data(0);
- }
+ assert(m_SP + count <= &data_stack[data_stack.size() - 1]);
+ std::fill(m_SP, m_SP + count, 0);
+ m_SP += count;
break;
}
case OpCodes::Print:
@@ -199,12 +206,9 @@ void Spasm::push(reg_t reg)
push_data(get_local(reg));
}
-/*!
-** Pops a data_t object from the data stack.
-*/
-void Spasm::pop()
+void Spasm::popto(reg_t reg)
{
- --m_SP;
+ m_FP[reg] = *(--m_SP);
}
/*!
diff --git a/spasm/src/spasm.hpp b/spasm/src/spasm.hpp
index 85b2e8d..01015d3 100644
--- a/spasm/src/spasm.hpp
+++ b/spasm/src/spasm.hpp
@@ -1,14 +1,14 @@
-#ifndef SPASM_HPP
-#define SPASM_HPP
-
-#include "spasm_impl.hpp"
-
-namespace Spasm
-{
-using SpasmImpl::byte;
-using SpasmImpl::PC_t;
-using SpasmImpl::Spasm;
-using SpasmImpl::OpCodes;
-} // namespace Spasm
-
-#endif // #ifndef SPASM_HPP
+#ifndef SPASM_HPP
+#define SPASM_HPP
+
+#include "spasm_impl.hpp"
+
+namespace Spasm
+{
+using SpasmImpl::byte;
+using SpasmImpl::OpCodes;
+using SpasmImpl::PC_t;
+using SpasmImpl::Spasm;
+} // namespace Spasm
+
+#endif // #ifndef SPASM_HPP
diff --git a/spasm/src/spasm_impl.hpp b/spasm/src/spasm_impl.hpp
index 75fb4c4..7c2bd38 100644
--- a/spasm/src/spasm_impl.hpp
+++ b/spasm/src/spasm_impl.hpp
@@ -13,8 +13,8 @@ enum OpCodes : char
Dup,
Pop,
PopTo,
+ PushFrom,
Push,
- Alloc,
Print,
Read,
Call,
@@ -45,14 +45,15 @@ class Spasm
public:
Spasm();
void Initialize(PC_t,
- const byte*,
- std::istream& = std::cin,
- std::ostream& = std::cout);
+ const byte*,
+ std::istream& = std::cin,
+ std::ostream& = std::cout);
~Spasm();
Spasm(const Spasm&) = delete;
Spasm& operator=(const Spasm&) = delete;
- enum RunResult {
+ enum RunResult
+ {
Success,
Exception,
NotImplemented,
@@ -77,7 +78,8 @@ class Spasm
//! Frame pointer - the start of the stack for the current function
data_t* m_FP = nullptr;
- struct Frame {
+ struct Frame
+ {
PC_t ReturnAddress;
PC_t FramePointer;
PC_t StackPointer;
@@ -94,8 +96,7 @@ class Spasm
std::ostream* ostr;
void push(reg_t reg);
- void pop(reg_t reg);
- void pop();
+ void popto(reg_t reg);
void dup();
void print(reg_t reg);