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());
}
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();
vm.run();
readln();

View File

@@ -160,3 +160,14 @@ class Print : Statement
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['\r'] = TokenType.NewLine;
table['\n'] = TokenType.NewLine;
table['?'] = TokenType.Print;
table['='] = TokenType.Assign;
reserved["OR"] = TokenType.Or;
reserved["AND"] = TokenType.And;
reserved["XOR"] = TokenType.Xor;
@@ -292,56 +294,19 @@ class Parser
switch(token.type)
{
case TokenType.Print:
return print();
case TokenType.Iden:
{
auto print = new Print();
bool addline = true;
wstring name = token.value.stringValue;
lex.popFront();
token = lex.front();
while(true)
if(token.type == TokenType.Assign)
{
//3号から厳密になって必ずいる
if(token.type == TokenType.Colon || token.type == TokenType.NewLine)
{
print.addLine();
break;
}
addline = true;
auto exp = expression();
if(exp is null)
{
syntaxError();
continue;
}
print.addArgument(exp);
token = lex.front();
if(lex.empty())
{
print.addLine();
break;
}
if(token.type != TokenType.Colon && token.type != TokenType.NewLine && token.type != TokenType.Semicolon && token.type != TokenType.Comma)
{
syntaxError();
}
else
{
lex.popFront();
if(token.type == TokenType.Semicolon)
{
addline = false;
continue;
}
if(token.type == TokenType.Comma)
{
print.addTab();
addline = false;
continue;
}
token = lex.front();
}
return assign(name);
}
return print;
//命令呼び出し
}
break;
case TokenType.Colon:
case TokenType.NewLine:
break;
@@ -351,6 +316,66 @@ class Parser
}
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();
bool addline = true;
lex.popFront();
auto token = lex.front();
while(true)
{
//3号から厳密になって必ずいる
if(token.type == TokenType.Colon || token.type == TokenType.NewLine)
{
print.addLine();
break;
}
addline = true;
auto exp = expression();
if(exp is null)
{
syntaxError();
continue;
}
print.addArgument(exp);
token = lex.front();
if(lex.empty())
{
print.addLine();
break;
}
if(token.type != TokenType.Colon && token.type != TokenType.NewLine && token.type != TokenType.Semicolon && token.type != TokenType.Comma)
{
syntaxError();
}
else
{
lex.popFront();
if(token.type == TokenType.Semicolon)
{
addline = false;
continue;
}
if(token.type == TokenType.Comma)
{
print.addTab();
addline = false;
continue;
}
token = lex.front();
}
}
return print;
}
Expression expression()
{
Expression node = null;

View File

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