1
0

予約語を扱えるようにした

This commit is contained in:
otya128
2015-06-04 19:31:47 +09:00
parent f9d52680f4
commit f453d86a79
2 changed files with 43 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import std.stdio;
class Lexical class Lexical
{ {
TokenType[] table; TokenType[] table;
TokenType[wstring] reserved;
wstring code; wstring code;
int index; int index;
this(wstring input) this(wstring input)
@@ -24,11 +25,23 @@ class Lexical
table['('] = TokenType.LParen; table['('] = TokenType.LParen;
table[')'] = TokenType.RParen; table[')'] = TokenType.RParen;
table[','] = TokenType.Comma; 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() bool empty()
{ {
return index >= code.length; return index >= code.length;
} }
bool isSmileBasicSuffix(wchar c)
{
return c == '$' || c == '%' || c == '#';
}
Token token; Token token;
void popFront() void popFront()
{ {
@@ -69,6 +82,19 @@ class Lexical
} }
iden ~= c; 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)); token = Token(TokenType.Iden, Value(iden));
break; break;
} }
@@ -156,6 +182,9 @@ class Parser
return 0;//TODO:エラーにすべきかは実装次第 return 0;//TODO:エラーにすべきかは実装次第
} }
} }
/*
テスト用
*/
int calc() int calc()
{ {
lex.popFront(); lex.popFront();
@@ -209,6 +238,17 @@ class Parser
return - - -1; return - - -1;
} }
} }
Statements parseProgram()
{
auto statements = new Statements();
statement();
return statements;
}
//statement
Statement statement()
{
return null;
}
Expression expression() Expression expression()
{ {
Expression node = null; Expression node = null;

View File

@@ -14,12 +14,15 @@ enum TokenType
Or, Or,
And, And,
Xor, Xor,
Not,
Print, Print,
If, If,
LParen, LParen,
RParen, RParen,
Iden, Iden,
Comma, Comma,
Colon,
Semicolon,
} }
struct Token struct Token