Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / lexdump.cpp
diff options
context:
space:
mode:
Commitc3252e63c296d1133254f4973fe7e3bf52cc6c1a (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-05 23:34:00 +0200
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-05 23:34:00 +0200
Files67c6bfc067696562aded70f66d25899f3f70a1c8 /spasm/src/asm/lexdump.cpp
IPL-c3252e63c296d1133254f4973fe7e3bf52cc6c1a.tar
IPL-c3252e63c296d1133254f4973fe7e3bf52cc6c1a.tar.gz
IPL-c3252e63c296d1133254f4973fe7e3bf52cc6c1a.zip
Parentc9912fdb174e1580629e00cacab6bc4ed480e643 (diff)
Parent6e19bb560d474736d45f5be011ff0830c7019991 (diff)
Merge commit '6e19bb560d474736d45f5be011ff0830c7019991' as 'spasm'
1 files changed, 83 insertions, 0 deletions
-rw-r--r--spasm/src/asm/lexdump.cpp83
diff --git a/spasm/src/asm/lexdump.cpp b/spasm/src/asm/lexdump.cpp
new file mode 100644
index 0000000..440ff1e
--- /dev/null
+++ b/spasm/src/asm/lexdump.cpp
@@ -0,0 +1,83 @@
+#include <iomanip>
+#include <iostream>
+
+#include <cassert>
+
+#include "lexer.hpp"
+#include "token.hpp"
+
+using namespace SpasmImpl::ASM::Lexer;
+
+#define TOKENS \
+ TOK(push) \
+ TOK(pop) \
+ TOK(dup) \
+ TOK(print) \
+ TOK(read) \
+ TOK(plus) \
+ TOK(minus) \
+ TOK(multiply) \
+ TOK(divide) \
+ TOK(modulus) \
+ TOK(gotrue) \
+ TOK(gofalse) \
+ TOK(go) \
+ TOK(call) \
+ TOK(ret) \
+ TOK(load) \
+ TOK(store) \
+ TOK(label) \
+ TOK(ident) \
+ TOK(integer) \
+ TOK(xinteger) \
+ TOK(endinput)
+
+class TokenDumper : public TokenStream
+{
+ public:
+ TokenDumper() : end_reached(false) {}
+ void push_token(const Token& token)
+ {
+ Token::Token_type ttype = token.type();
+ std::cout.width(8);
+ std::cout << token.lineno();
+ std::cout << ": " << token_name[ttype];
+ if (ttype == Token::integer || ttype == Token::xinteger)
+ std::cout << '|' << token.value_int() << '|';
+ else if (ttype == Token::ident)
+ std::cout << '|' << token.value_str() << '|';
+ std::cout << std::endl;
+ if (token.type() == Token::endinput)
+ end_reached = true;
+ }
+
+ bool input_end() const { return end_reached; }
+
+ private:
+ bool end_reached;
+
+ static const char* token_name[24];
+};
+
+const char* TokenDumper::token_name[24] = {"halt",
+#define TOK(x) #x,
+ TOKENS
+#undef TOK
+ "notused!!!"};
+
+int main()
+{
+ Lexer lex(std::cin);
+ TokenDumper td;
+ Token token;
+
+ lex.buffer_init();
+
+ while (!td.input_end())
+ {
+ lex.read(1);
+ lex.tokenize(td);
+ }
+
+ return 0;
+}