From f453d86a79ec5b7ddc07379acab6f31ab1e5fece Mon Sep 17 00:00:00 2001 From: otya128 Date: Thu, 4 Jun 2015 19:31:47 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BA=88=E7=B4=84=E8=AA=9E=E3=82=92=E6=89=B1?= =?UTF-8?q?=E3=81=88=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/parser.d | 40 ++++++++++++++++++++++++++++++++++++++++ SMILEBASIC/token.d | 3 +++ 2 files changed, 43 insertions(+) diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index d499a7d..178a9bb 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -7,6 +7,7 @@ import std.stdio; class Lexical { TokenType[] table; + TokenType[wstring] reserved; wstring code; int index; this(wstring input) @@ -24,11 +25,23 @@ class Lexical table['('] = TokenType.LParen; table[')'] = TokenType.RParen; table[','] = TokenType.Comma; + table[':'] = TokenType.Colon; + table[';'] = TokenType.Semicolon; + reserved["OR"] = TokenType.Or; + reserved["AND"] = TokenType.And; + reserved["XOR"] = TokenType.Xor; + reserved["NOT"] = TokenType.Not; + reserved["PRINT"] = TokenType.Print; + reserved.rehash(); } bool empty() { return index >= code.length; } + bool isSmileBasicSuffix(wchar c) + { + return c == '$' || c == '%' || c == '#'; + } Token token; void popFront() { @@ -69,6 +82,19 @@ class Lexical } iden ~= c; } + if(isSmileBasicSuffix(c)) + { + iden ~= c; + i++; + } + else//変数が予約語であることはありえない + { + auto r = reserved.get(iden, TokenType.Unknown); + if(r != TokenType.Unknown) + { + token = Token(r); + } + } token = Token(TokenType.Iden, Value(iden)); break; } @@ -156,6 +182,9 @@ class Parser return 0;//TODO:エラーにすべきかは実装次第 } } + /* + テスト用 + */ int calc() { lex.popFront(); @@ -209,6 +238,17 @@ class Parser return - - -1; } } + Statements parseProgram() + { + auto statements = new Statements(); + statement(); + return statements; + } + //statement + Statement statement() + { + return null; + } Expression expression() { Expression node = null; diff --git a/SMILEBASIC/token.d b/SMILEBASIC/token.d index 2efc607..8720362 100644 --- a/SMILEBASIC/token.d +++ b/SMILEBASIC/token.d @@ -14,12 +14,15 @@ enum TokenType Or, And, Xor, + Not, Print, If, LParen, RParen, Iden, Comma, + Colon, + Semicolon, } struct Token