diff --git a/SMILEBASIC/main.d b/SMILEBASIC/main.d index 3740fc6..eb91f41 100644 --- a/SMILEBASIC/main.d +++ b/SMILEBASIC/main.d @@ -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(); diff --git a/SMILEBASIC/node.d b/SMILEBASIC/node.d index 011ed27..77d267b 100644 --- a/SMILEBASIC/node.d +++ b/SMILEBASIC/node.d @@ -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; + } +} diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index ccdc913..bd7361b 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -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; diff --git a/SMILEBASIC/token.d b/SMILEBASIC/token.d index f2fc8fd..1b38f86 100644 --- a/SMILEBASIC/token.d +++ b/SMILEBASIC/token.d @@ -24,6 +24,7 @@ enum TokenType Colon, Semicolon, NewLine, + Assign, } struct Token