1
0

代入の構文解析を実�

This commit is contained in:
otya128
2015-06-05 21:30:43 +09:00
parent 04f35b3801
commit edd9affacc
4 changed files with 82 additions and 45 deletions

View File

@@ -8,7 +8,7 @@ int main(string[] argv)
writeln(parser.calc()); writeln(parser.calc());
} }
auto parser = new Parser("PRINT 1+1,2+3;10-5,A"); auto parser = new Parser("A=1\nPRINT 1+1,2+3;10-5,A");
auto vm = parser.compile(); auto vm = parser.compile();
vm.run(); vm.run();
readln(); readln();

View File

@@ -160,3 +160,14 @@ class Print : Statement
args ~= PrintArgument(PrintArgumentType.Line); args ~= PrintArgument(PrintArgumentType.Line);
} }
} }
class Assign : Statement
{
wstring name;
Expression expr;
this(wstring name, Expression expr)
{
this.type = NodeType.Assign;
this.name = name;
this.expr = expr;
}
}

View File

@@ -31,6 +31,8 @@ class Lexical
table[';'] = TokenType.Semicolon; table[';'] = TokenType.Semicolon;
table['\r'] = TokenType.NewLine; table['\r'] = TokenType.NewLine;
table['\n'] = TokenType.NewLine; table['\n'] = TokenType.NewLine;
table['?'] = TokenType.Print;
table['='] = TokenType.Assign;
reserved["OR"] = TokenType.Or; reserved["OR"] = TokenType.Or;
reserved["AND"] = TokenType.And; reserved["AND"] = TokenType.And;
reserved["XOR"] = TokenType.Xor; reserved["XOR"] = TokenType.Xor;
@@ -292,11 +294,43 @@ class Parser
switch(token.type) switch(token.type)
{ {
case TokenType.Print: case TokenType.Print:
return print();
case TokenType.Iden:
{
wstring name = token.value.stringValue;
lex.popFront();
token = lex.front();
if(token.type == TokenType.Assign)
{
return assign(name);
}
//命令呼び出し
}
break;
case TokenType.Colon:
case TokenType.NewLine:
break;
default:
syntaxError();
break;
}
return null;
}
Assign assign(wstring name)
{
lex.popFront();
auto token = lex.front();//=の次
Expression expr = expression();
if(expr is null) return null;
auto a = new Assign(name, expr);
return a;
}
Print print()
{ {
auto print = new Print(); auto print = new Print();
bool addline = true; bool addline = true;
lex.popFront(); lex.popFront();
token = lex.front(); auto token = lex.front();
while(true) while(true)
{ {
//3号から厳密になって必ずいる //3号から厳密になって必ずいる
@@ -342,15 +376,6 @@ class Parser
} }
return print; return print;
} }
case TokenType.Colon:
case TokenType.NewLine:
break;
default:
syntaxError();
break;
}
return null;
}
Expression expression() Expression expression()
{ {
Expression node = null; Expression node = null;

View File

@@ -24,6 +24,7 @@ enum TokenType
Colon, Colon,
Semicolon, Semicolon,
NewLine, NewLine,
Assign,
} }
struct Token struct Token