зеркало из https://github.com/mozilla/pjs.git
Updated lexer API
This commit is contained in:
Родитель
cf09318769
Коммит
a84c5b0547
166
js/js2/js2.cpp
166
js/js2/js2.cpp
|
@ -23,7 +23,6 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdio>
|
|
||||||
//#include <stdlib.h>
|
//#include <stdlib.h>
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
namespace JS = JavaScript;
|
namespace JS = JavaScript;
|
||||||
|
@ -149,7 +148,7 @@ static void readEvalPrint(istream &in, World &world)
|
||||||
{
|
{
|
||||||
String buffer;
|
String buffer;
|
||||||
string line;
|
string line;
|
||||||
String source = widenCString("console");
|
String sourceLocation = widenCString("console");
|
||||||
|
|
||||||
while (promptLine(in, line, buffer.empty() ? "js> " : "")) {
|
while (promptLine(in, line, buffer.empty() ? "js> " : "")) {
|
||||||
if (!buffer.empty())
|
if (!buffer.empty())
|
||||||
|
@ -157,11 +156,10 @@ static void readEvalPrint(istream &in, World &world)
|
||||||
appendChars(buffer, line.data(), line.size());
|
appendChars(buffer, line.data(), line.size());
|
||||||
if (!buffer.empty()) {
|
if (!buffer.empty()) {
|
||||||
try {
|
try {
|
||||||
StringReader r(buffer, source);
|
Lexer l(world, buffer, sourceLocation);
|
||||||
Lexer l(r, world);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Token &t = l.get(true);
|
const Token &t = l.get(true);
|
||||||
if (t.kind == Token::End)
|
if (t.hasKind(Token::end))
|
||||||
break;
|
break;
|
||||||
String out;
|
String out;
|
||||||
out += ' ';
|
out += ' ';
|
||||||
|
@ -330,104 +328,104 @@ ProcessArgs(char **argv, int argc)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "icodegenerator.h"
|
#include "icodegenerator.h"
|
||||||
void testICG(World &world)
|
static void testICG(World &world)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// testing ICG
|
// testing ICG
|
||||||
//
|
//
|
||||||
SourcePosition pos = { 0, 0, 0 };
|
uint32 pos = 0;
|
||||||
ICodeGenerator icg;
|
ICodeGenerator icg;
|
||||||
|
|
||||||
// var i,j; i = j + 2;
|
// var i,j; i = j + 2;
|
||||||
// i is bound to var #0, j to var #1
|
// i is bound to var #0, j to var #1
|
||||||
|
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
Register r1 = icg.loadVariable(1);
|
Register r1 = icg.loadVariable(1);
|
||||||
Register r2 = icg.loadImmediate(2.0);
|
Register r2 = icg.loadImmediate(2.0);
|
||||||
icg.saveVariable(0, icg.op(ADD, r1, r2));
|
icg.saveVariable(0, icg.op(ADD, r1, r2));
|
||||||
|
|
||||||
// j = a.b
|
// j = a.b
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
Register r4 = icg.loadName(world.identifiers[widenCString("a")]);
|
Register r4 = icg.loadName(world.identifiers[widenCString("a")]);
|
||||||
Register r5 = icg.getProperty(world.identifiers[widenCString("b")], r4);
|
Register r5 = icg.getProperty(world.identifiers[widenCString("b")], r4);
|
||||||
icg.saveVariable(1, r5);
|
icg.saveVariable(1, r5);
|
||||||
|
|
||||||
// while (i) i = i + j;
|
// while (i) i = i + j;
|
||||||
icg.beginWhileStatement(pos);
|
icg.beginWhileStatement(pos);
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.endWhileExpression(r1);
|
icg.endWhileExpression(r1);
|
||||||
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadVariable(1)));
|
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadVariable(1)));
|
||||||
icg.endWhileStatement();
|
icg.endWhileStatement();
|
||||||
|
|
||||||
// if (i) if (j) i = 3; else j = 4;
|
// if (i) if (j) i = 3; else j = 4;
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.beginIfStatement(pos, r1);
|
icg.beginIfStatement(pos, r1);
|
||||||
r2 = icg.loadVariable(1);
|
r2 = icg.loadVariable(1);
|
||||||
icg.beginIfStatement(pos, r2);
|
icg.beginIfStatement(pos, r2);
|
||||||
icg.saveVariable(0, icg.loadImmediate(3));
|
icg.saveVariable(0, icg.loadImmediate(3));
|
||||||
icg.beginElseStatement(true);
|
icg.beginElseStatement(true);
|
||||||
icg.saveVariable(1, icg.loadImmediate(4));
|
icg.saveVariable(1, icg.loadImmediate(4));
|
||||||
icg.endIfStatement();
|
|
||||||
icg.beginElseStatement(false);
|
|
||||||
icg.endIfStatement();
|
icg.endIfStatement();
|
||||||
|
icg.beginElseStatement(false);
|
||||||
|
icg.endIfStatement();
|
||||||
|
|
||||||
|
|
||||||
// switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; }
|
// switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; }
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.beginSwitchStatement(pos, r1);
|
icg.beginSwitchStatement(pos, r1);
|
||||||
// case 3, note empty case statement (?necessary???)
|
// case 3, note empty case statement (?necessary???)
|
||||||
icg.endCaseCondition(icg.loadImmediate(3));
|
icg.endCaseCondition(icg.loadImmediate(3));
|
||||||
icg.beginCaseStatement();
|
icg.beginCaseStatement();
|
||||||
icg.endCaseStatement();
|
icg.endCaseStatement();
|
||||||
// case 4
|
// case 4
|
||||||
icg.endCaseCondition(icg.loadImmediate(4));
|
icg.endCaseCondition(icg.loadImmediate(4));
|
||||||
icg.beginCaseStatement();
|
icg.beginCaseStatement();
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
icg.saveVariable(1, icg.loadImmediate(4));
|
icg.saveVariable(1, icg.loadImmediate(4));
|
||||||
icg.breakStatement();
|
icg.breakStatement();
|
||||||
icg.endCaseStatement();
|
icg.endCaseStatement();
|
||||||
// case 5
|
// case 5
|
||||||
icg.endCaseCondition(icg.loadImmediate(5));
|
icg.endCaseCondition(icg.loadImmediate(5));
|
||||||
icg.beginCaseStatement();
|
icg.beginCaseStatement();
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
icg.saveVariable(1, icg.loadImmediate(5));
|
icg.saveVariable(1, icg.loadImmediate(5));
|
||||||
icg.breakStatement();
|
icg.breakStatement();
|
||||||
icg.endCaseStatement();
|
icg.endCaseStatement();
|
||||||
// default
|
// default
|
||||||
icg.beginDefaultStatement();
|
icg.beginDefaultStatement();
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
icg.saveVariable(1, icg.loadImmediate(6));
|
icg.saveVariable(1, icg.loadImmediate(6));
|
||||||
icg.endDefaultStatement();
|
icg.endDefaultStatement();
|
||||||
icg.endSwitchStatement();
|
icg.endSwitchStatement();
|
||||||
|
|
||||||
// for ( ; i; i + 1 ) j = 99;
|
// for ( ; i; i + 1 ) j = 99;
|
||||||
icg.beginForStatement(pos);
|
icg.beginForStatement(pos);
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.forCondition(r1);
|
icg.forCondition(r1);
|
||||||
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadImmediate(1)));
|
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadImmediate(1)));
|
||||||
icg.forIncrement();
|
icg.forIncrement();
|
||||||
icg.saveVariable(0, icg.loadImmediate(99));
|
icg.saveVariable(0, icg.loadImmediate(99));
|
||||||
icg.endForStatement();
|
icg.endForStatement();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
InstructionStream *iCode = icg.complete();
|
InstructionStream *iCode = icg.complete();
|
||||||
|
|
||||||
std::cout << icg;
|
std::cout << icg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
||||||
initConsole("\pJavaScript Shell", "Welcome to the js2 shell.\n", argc, argv);
|
initConsole("\pJavaScript Shell", "Welcome to the js2 shell.\n", argc, argv);
|
||||||
#endif
|
#endif
|
||||||
World world;
|
World world;
|
||||||
#if 1
|
#if 0
|
||||||
testICG(world);
|
testICG(world);
|
||||||
#else
|
#else
|
||||||
readEvalPrint(std::cin, world);
|
readEvalPrint(std::cin, world);
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
//return ProcessArgs(argv + 1, argc - 1);
|
//return ProcessArgs(argv + 1, argc - 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdio>
|
|
||||||
//#include <stdlib.h>
|
//#include <stdlib.h>
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
namespace JS = JavaScript;
|
namespace JS = JavaScript;
|
||||||
|
@ -149,7 +148,7 @@ static void readEvalPrint(istream &in, World &world)
|
||||||
{
|
{
|
||||||
String buffer;
|
String buffer;
|
||||||
string line;
|
string line;
|
||||||
String source = widenCString("console");
|
String sourceLocation = widenCString("console");
|
||||||
|
|
||||||
while (promptLine(in, line, buffer.empty() ? "js> " : "")) {
|
while (promptLine(in, line, buffer.empty() ? "js> " : "")) {
|
||||||
if (!buffer.empty())
|
if (!buffer.empty())
|
||||||
|
@ -157,11 +156,10 @@ static void readEvalPrint(istream &in, World &world)
|
||||||
appendChars(buffer, line.data(), line.size());
|
appendChars(buffer, line.data(), line.size());
|
||||||
if (!buffer.empty()) {
|
if (!buffer.empty()) {
|
||||||
try {
|
try {
|
||||||
StringReader r(buffer, source);
|
Lexer l(world, buffer, sourceLocation);
|
||||||
Lexer l(r, world);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Token &t = l.get(true);
|
const Token &t = l.get(true);
|
||||||
if (t.kind == Token::End)
|
if (t.hasKind(Token::end))
|
||||||
break;
|
break;
|
||||||
String out;
|
String out;
|
||||||
out += ' ';
|
out += ' ';
|
||||||
|
@ -330,104 +328,104 @@ ProcessArgs(char **argv, int argc)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "icodegenerator.h"
|
#include "icodegenerator.h"
|
||||||
void testICG(World &world)
|
static void testICG(World &world)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// testing ICG
|
// testing ICG
|
||||||
//
|
//
|
||||||
SourcePosition pos = { 0, 0, 0 };
|
uint32 pos = 0;
|
||||||
ICodeGenerator icg;
|
ICodeGenerator icg;
|
||||||
|
|
||||||
// var i,j; i = j + 2;
|
// var i,j; i = j + 2;
|
||||||
// i is bound to var #0, j to var #1
|
// i is bound to var #0, j to var #1
|
||||||
|
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
Register r1 = icg.loadVariable(1);
|
Register r1 = icg.loadVariable(1);
|
||||||
Register r2 = icg.loadImmediate(2.0);
|
Register r2 = icg.loadImmediate(2.0);
|
||||||
icg.saveVariable(0, icg.op(ADD, r1, r2));
|
icg.saveVariable(0, icg.op(ADD, r1, r2));
|
||||||
|
|
||||||
// j = a.b
|
// j = a.b
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
Register r4 = icg.loadName(world.identifiers[widenCString("a")]);
|
Register r4 = icg.loadName(world.identifiers[widenCString("a")]);
|
||||||
Register r5 = icg.getProperty(world.identifiers[widenCString("b")], r4);
|
Register r5 = icg.getProperty(world.identifiers[widenCString("b")], r4);
|
||||||
icg.saveVariable(1, r5);
|
icg.saveVariable(1, r5);
|
||||||
|
|
||||||
// while (i) i = i + j;
|
// while (i) i = i + j;
|
||||||
icg.beginWhileStatement(pos);
|
icg.beginWhileStatement(pos);
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.endWhileExpression(r1);
|
icg.endWhileExpression(r1);
|
||||||
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadVariable(1)));
|
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadVariable(1)));
|
||||||
icg.endWhileStatement();
|
icg.endWhileStatement();
|
||||||
|
|
||||||
// if (i) if (j) i = 3; else j = 4;
|
// if (i) if (j) i = 3; else j = 4;
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.beginIfStatement(pos, r1);
|
icg.beginIfStatement(pos, r1);
|
||||||
r2 = icg.loadVariable(1);
|
r2 = icg.loadVariable(1);
|
||||||
icg.beginIfStatement(pos, r2);
|
icg.beginIfStatement(pos, r2);
|
||||||
icg.saveVariable(0, icg.loadImmediate(3));
|
icg.saveVariable(0, icg.loadImmediate(3));
|
||||||
icg.beginElseStatement(true);
|
icg.beginElseStatement(true);
|
||||||
icg.saveVariable(1, icg.loadImmediate(4));
|
icg.saveVariable(1, icg.loadImmediate(4));
|
||||||
icg.endIfStatement();
|
|
||||||
icg.beginElseStatement(false);
|
|
||||||
icg.endIfStatement();
|
icg.endIfStatement();
|
||||||
|
icg.beginElseStatement(false);
|
||||||
|
icg.endIfStatement();
|
||||||
|
|
||||||
|
|
||||||
// switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; }
|
// switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; }
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.beginSwitchStatement(pos, r1);
|
icg.beginSwitchStatement(pos, r1);
|
||||||
// case 3, note empty case statement (?necessary???)
|
// case 3, note empty case statement (?necessary???)
|
||||||
icg.endCaseCondition(icg.loadImmediate(3));
|
icg.endCaseCondition(icg.loadImmediate(3));
|
||||||
icg.beginCaseStatement();
|
icg.beginCaseStatement();
|
||||||
icg.endCaseStatement();
|
icg.endCaseStatement();
|
||||||
// case 4
|
// case 4
|
||||||
icg.endCaseCondition(icg.loadImmediate(4));
|
icg.endCaseCondition(icg.loadImmediate(4));
|
||||||
icg.beginCaseStatement();
|
icg.beginCaseStatement();
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
icg.saveVariable(1, icg.loadImmediate(4));
|
icg.saveVariable(1, icg.loadImmediate(4));
|
||||||
icg.breakStatement();
|
icg.breakStatement();
|
||||||
icg.endCaseStatement();
|
icg.endCaseStatement();
|
||||||
// case 5
|
// case 5
|
||||||
icg.endCaseCondition(icg.loadImmediate(5));
|
icg.endCaseCondition(icg.loadImmediate(5));
|
||||||
icg.beginCaseStatement();
|
icg.beginCaseStatement();
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
icg.saveVariable(1, icg.loadImmediate(5));
|
icg.saveVariable(1, icg.loadImmediate(5));
|
||||||
icg.breakStatement();
|
icg.breakStatement();
|
||||||
icg.endCaseStatement();
|
icg.endCaseStatement();
|
||||||
// default
|
// default
|
||||||
icg.beginDefaultStatement();
|
icg.beginDefaultStatement();
|
||||||
icg.beginStatement(pos);
|
icg.beginStatement(pos);
|
||||||
icg.saveVariable(1, icg.loadImmediate(6));
|
icg.saveVariable(1, icg.loadImmediate(6));
|
||||||
icg.endDefaultStatement();
|
icg.endDefaultStatement();
|
||||||
icg.endSwitchStatement();
|
icg.endSwitchStatement();
|
||||||
|
|
||||||
// for ( ; i; i + 1 ) j = 99;
|
// for ( ; i; i + 1 ) j = 99;
|
||||||
icg.beginForStatement(pos);
|
icg.beginForStatement(pos);
|
||||||
r1 = icg.loadVariable(0);
|
r1 = icg.loadVariable(0);
|
||||||
icg.forCondition(r1);
|
icg.forCondition(r1);
|
||||||
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadImmediate(1)));
|
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadImmediate(1)));
|
||||||
icg.forIncrement();
|
icg.forIncrement();
|
||||||
icg.saveVariable(0, icg.loadImmediate(99));
|
icg.saveVariable(0, icg.loadImmediate(99));
|
||||||
icg.endForStatement();
|
icg.endForStatement();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
InstructionStream *iCode = icg.complete();
|
InstructionStream *iCode = icg.complete();
|
||||||
|
|
||||||
std::cout << icg;
|
std::cout << icg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
||||||
initConsole("\pJavaScript Shell", "Welcome to the js2 shell.\n", argc, argv);
|
initConsole("\pJavaScript Shell", "Welcome to the js2 shell.\n", argc, argv);
|
||||||
#endif
|
#endif
|
||||||
World world;
|
World world;
|
||||||
#if 1
|
#if 0
|
||||||
testICG(world);
|
testICG(world);
|
||||||
#else
|
#else
|
||||||
readEvalPrint(std::cin, world);
|
readEvalPrint(std::cin, world);
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
//return ProcessArgs(argv + 1, argc - 1);
|
//return ProcessArgs(argv + 1, argc - 1);
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче