2000-01-11 00:22:43 +03:00
|
|
|
// -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
//
|
|
|
|
// The contents of this file are subject to the Netscape Public
|
|
|
|
// License Version 1.1 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at http://www.mozilla.org/NPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS
|
|
|
|
// IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
|
|
|
// implied. See the License for the specific language governing
|
|
|
|
// rights and limitations under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the JavaScript 2 Prototype.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is Netscape
|
|
|
|
// Communications Corporation. Portions created by Netscape are
|
|
|
|
// Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
// Rights Reserved.
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// JS2 shell.
|
|
|
|
//
|
|
|
|
|
2000-04-07 06:58:01 +04:00
|
|
|
#include <assert.h>
|
|
|
|
|
2000-01-11 00:22:43 +03:00
|
|
|
#include "world.h"
|
2000-04-06 06:59:16 +04:00
|
|
|
#include "interpreter.h"
|
2000-04-21 04:37:51 +04:00
|
|
|
#include "icodegenerator.h"
|
2000-04-06 06:59:16 +04:00
|
|
|
|
2000-04-29 04:23:06 +04:00
|
|
|
#include "debugger.h"
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-07 03:47:33 +04:00
|
|
|
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
2000-01-11 00:22:43 +03:00
|
|
|
#include <SIOUX.h>
|
|
|
|
#include <MacTypes.h>
|
|
|
|
|
2000-04-18 04:51:53 +04:00
|
|
|
static char *mac_argv[] = {"js2", 0};
|
|
|
|
|
|
|
|
static void initConsole(StringPtr consoleName,
|
|
|
|
const char* startupMessage,
|
|
|
|
int &argc, char **&argv)
|
|
|
|
{
|
|
|
|
SIOUXSettings.autocloseonquit = false;
|
|
|
|
SIOUXSettings.asktosaveonclose = false;
|
|
|
|
SIOUXSetTitle(consoleName);
|
|
|
|
|
|
|
|
// Set up a buffer for stderr (otherwise it's a pig).
|
|
|
|
static char buffer[BUFSIZ];
|
|
|
|
setvbuf(stderr, buffer, _IOLBF, BUFSIZ);
|
|
|
|
|
|
|
|
JavaScript::stdOut << startupMessage;
|
|
|
|
|
|
|
|
argc = 1;
|
|
|
|
argv = mac_argv;
|
|
|
|
}
|
2000-04-19 06:09:06 +04:00
|
|
|
|
2000-04-07 03:47:33 +04:00
|
|
|
#endif
|
2000-01-11 00:22:43 +03:00
|
|
|
|
2000-04-18 04:51:53 +04:00
|
|
|
namespace JavaScript {
|
2000-04-21 04:04:14 +04:00
|
|
|
namespace Shell {
|
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
using namespace ICG;
|
|
|
|
using namespace JSTypes;
|
|
|
|
using namespace Interpreter;
|
|
|
|
|
|
|
|
// Interactively read a line from the input stream in and put it into
|
|
|
|
// s. Return false if reached the end of input before reading anything.
|
2000-05-12 09:15:52 +04:00
|
|
|
static bool promptLine(LineReader &inReader, string &s, const char *prompt)
|
2000-04-26 09:37:00 +04:00
|
|
|
{
|
|
|
|
if (prompt) {
|
|
|
|
stdOut << prompt;
|
2000-05-12 09:15:52 +04:00
|
|
|
#ifdef XP_MAC_MPW
|
2000-04-26 09:37:00 +04:00
|
|
|
// Print a CR after the prompt because MPW grabs the entire
|
|
|
|
// line when entering an interactive command.
|
|
|
|
stdOut << '\n';
|
2000-05-12 09:15:52 +04:00
|
|
|
#endif
|
2000-04-21 04:04:14 +04:00
|
|
|
}
|
2000-04-26 09:37:00 +04:00
|
|
|
return inReader.readLine(s) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-29 04:23:06 +04:00
|
|
|
JavaScript::World world;
|
2000-05-05 08:45:39 +04:00
|
|
|
JavaScript::Debugger::Shell jsd(world, stdin, JavaScript::stdOut,
|
2000-05-05 02:42:49 +04:00
|
|
|
JavaScript::stdOut);
|
2000-05-09 23:01:00 +04:00
|
|
|
|
2000-05-09 09:46:26 +04:00
|
|
|
const bool showTokens = false;
|
2000-04-26 09:37:00 +04:00
|
|
|
|
2000-05-09 03:12:10 +04:00
|
|
|
|
2000-05-09 23:01:00 +04:00
|
|
|
static JSValue print(const JSValues &argv)
|
|
|
|
{
|
|
|
|
size_t n = argv.size();
|
|
|
|
if (n > 0) {
|
|
|
|
stdOut << argv[0];
|
|
|
|
for (size_t i = 1; i < n; ++i)
|
|
|
|
stdOut << ' ' << argv[i];
|
|
|
|
}
|
|
|
|
stdOut << "\n";
|
|
|
|
return kUndefinedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-24 06:11:39 +04:00
|
|
|
static void genCode(World &world, Context &cx, StmtNode *p)
|
2000-05-09 03:12:10 +04:00
|
|
|
{
|
2000-05-18 04:01:33 +04:00
|
|
|
ICodeGenerator icg(&world);
|
2000-05-24 06:11:39 +04:00
|
|
|
Register ret = NotARegister;
|
|
|
|
while (p) {
|
|
|
|
ret = icg.genStmt(p);
|
|
|
|
p = p->next;
|
|
|
|
}
|
2000-05-27 02:34:42 +04:00
|
|
|
icg.returnStmt(ret);
|
2000-05-18 04:01:33 +04:00
|
|
|
stdOut << '\n';
|
2000-05-09 03:12:10 +04:00
|
|
|
stdOut << icg;
|
|
|
|
JSValue result = cx.interpret(icg.complete(), JSValues());
|
2000-05-09 09:46:26 +04:00
|
|
|
stdOut << "result = " << result << "\n";
|
2000-05-09 03:12:10 +04:00
|
|
|
}
|
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
static void readEvalPrint(FILE *in, World &world)
|
|
|
|
{
|
2000-05-18 04:01:33 +04:00
|
|
|
JSScope glob;
|
|
|
|
Context cx(world, &glob);
|
|
|
|
StringAtom& printName = world.identifiers[widenCString("print")];
|
|
|
|
glob.defineNativeFunction(printName, print);
|
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
String buffer;
|
|
|
|
string line;
|
|
|
|
String sourceLocation = widenCString("console");
|
|
|
|
LineReader inReader(in);
|
|
|
|
|
2000-05-12 09:15:52 +04:00
|
|
|
while (promptLine(inReader, line, buffer.empty() ? "js> " : "> ")) {
|
2000-04-26 09:37:00 +04:00
|
|
|
appendChars(buffer, line.data(), line.size());
|
|
|
|
try {
|
|
|
|
Arena a;
|
|
|
|
Parser p(world, a, buffer, sourceLocation);
|
|
|
|
|
|
|
|
if (showTokens) {
|
|
|
|
Lexer &l = p.lexer;
|
|
|
|
while (true) {
|
|
|
|
const Token &t = l.get(true);
|
|
|
|
if (t.hasKind(Token::end))
|
|
|
|
break;
|
|
|
|
stdOut << ' ';
|
|
|
|
t.print(stdOut, true);
|
2000-04-21 04:04:14 +04:00
|
|
|
}
|
2000-05-24 05:51:32 +04:00
|
|
|
stdOut << '\n';
|
2000-04-26 09:37:00 +04:00
|
|
|
} else {
|
2000-05-24 05:51:32 +04:00
|
|
|
StmtNode *parsedStatements = p.parseProgram();
|
|
|
|
ASSERT(p.lexer.peek(true).hasKind(Token::end));
|
2000-05-12 07:26:43 +04:00
|
|
|
{
|
|
|
|
PrettyPrinter f(stdOut, 20);
|
|
|
|
{
|
|
|
|
PrettyPrinter::Block b(f, 2);
|
2000-05-24 05:51:32 +04:00
|
|
|
f << "Program =";
|
2000-05-12 07:26:43 +04:00
|
|
|
f.linearBreak(1);
|
2000-05-24 05:51:32 +04:00
|
|
|
StmtNode::printStatements(f, parsedStatements);
|
2000-05-12 07:26:43 +04:00
|
|
|
}
|
|
|
|
f.end();
|
|
|
|
}
|
2000-05-24 05:51:32 +04:00
|
|
|
stdOut << '\n';
|
|
|
|
#if 0
|
|
|
|
// Generate code for parsedStatements, which is a linked list of zero or more statements
|
2000-05-24 06:11:39 +04:00
|
|
|
genCode(world, cx, parsedStatements);
|
2000-05-18 04:01:33 +04:00
|
|
|
#endif
|
2000-04-26 09:37:00 +04:00
|
|
|
}
|
|
|
|
clear(buffer);
|
|
|
|
} catch (Exception &e) {
|
|
|
|
/* If we got a syntax error on the end of input,
|
|
|
|
* then wait for a continuation
|
|
|
|
* of input rather than printing the error message. */
|
|
|
|
if (!(e.hasKind(Exception::syntaxError) &&
|
|
|
|
e.lineNum && e.pos == buffer.size() &&
|
|
|
|
e.sourceFile == sourceLocation)) {
|
|
|
|
stdOut << '\n' << e.fullMessage();
|
2000-04-21 04:04:14 +04:00
|
|
|
clear(buffer);
|
2000-01-11 00:22:43 +03:00
|
|
|
}
|
|
|
|
}
|
2000-04-21 04:04:14 +04:00
|
|
|
}
|
2000-04-26 09:37:00 +04:00
|
|
|
stdOut << '\n';
|
|
|
|
}
|
2000-04-28 04:11:18 +04:00
|
|
|
|
2000-04-29 04:23:06 +04:00
|
|
|
|
2000-04-28 17:31:39 +04:00
|
|
|
/**
|
|
|
|
* Poor man's instruction tracing facility.
|
|
|
|
*/
|
|
|
|
class Tracer : public Context::Listener {
|
|
|
|
typedef InstructionStream::difference_type InstructionOffset;
|
2000-05-12 05:20:34 +04:00
|
|
|
void listen(Context* context, Context::Event event)
|
2000-04-28 17:31:39 +04:00
|
|
|
{
|
2000-05-12 05:20:34 +04:00
|
|
|
if (event & Context::EV_STEP) {
|
|
|
|
ICodeModule *iCode = context->getICode();
|
|
|
|
JSValues ®isters = context->getRegisters();
|
|
|
|
InstructionIterator pc = context->getPC();
|
|
|
|
|
|
|
|
|
|
|
|
InstructionOffset offset = (pc - iCode->its_iCode->begin());
|
|
|
|
printFormat(stdOut, "trace [%02u:%04u]: ",
|
|
|
|
iCode->mID, offset);
|
2000-05-05 02:42:49 +04:00
|
|
|
|
2000-05-12 05:20:34 +04:00
|
|
|
Instruction* i = *pc;
|
|
|
|
stdOut << *i;
|
|
|
|
if (i->op() != BRANCH && i->count() > 0) {
|
|
|
|
stdOut << " [";
|
|
|
|
i->printOperands(stdOut, registers);
|
|
|
|
stdOut << "]\n";
|
|
|
|
} else {
|
|
|
|
stdOut << '\n';
|
|
|
|
}
|
2000-04-28 17:31:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2000-05-09 03:12:10 +04:00
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
static float64 testFactorial(World &world, float64 n)
|
|
|
|
{
|
2000-04-29 18:14:28 +04:00
|
|
|
JSScope glob;
|
2000-04-26 09:37:00 +04:00
|
|
|
Context cx(world, &glob);
|
|
|
|
// generate code for factorial, and interpret it.
|
2000-05-24 06:11:39 +04:00
|
|
|
uint32 pos = 0;
|
2000-05-27 02:34:42 +04:00
|
|
|
ICodeGenerator icg(&world);
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
// fact(n) {
|
|
|
|
// var result = 1;
|
2000-04-19 06:09:06 +04:00
|
|
|
|
2000-05-24 06:11:39 +04:00
|
|
|
StringAtom &n_name = world.identifiers[widenCString("n")];
|
|
|
|
StringAtom &result_name = world.identifiers[widenCString("result")];
|
|
|
|
|
2000-05-27 02:34:42 +04:00
|
|
|
Register r_n = icg.allocateParameter(n_name);
|
2000-05-24 06:11:39 +04:00
|
|
|
Register r_result = icg.allocateVariable(result_name);
|
|
|
|
|
|
|
|
Arena a;
|
|
|
|
|
|
|
|
ExprStmtNode *e = new(a) ExprStmtNode(pos, StmtNode::expression, new(a) BinaryExprNode(pos, ExprNode::assignment,
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, result_name),
|
|
|
|
new(a) NumberExprNode(pos, 1.0) ) );
|
|
|
|
icg.genStmt(e);
|
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
// while (n > 1) {
|
|
|
|
// result = result * n;
|
|
|
|
// n = n - 1;
|
|
|
|
// }
|
|
|
|
{
|
2000-05-24 06:11:39 +04:00
|
|
|
BinaryExprNode *c = new(a) BinaryExprNode(pos, ExprNode::greaterThan,
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, n_name),
|
|
|
|
new(a) NumberExprNode(pos, 1.0) ) ;
|
|
|
|
ExprStmtNode *e1 = new(a) ExprStmtNode(pos, StmtNode::expression, new(a) BinaryExprNode(pos, ExprNode::assignment,
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, result_name),
|
|
|
|
new(a) BinaryExprNode(pos, ExprNode::multiply,
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, result_name),
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, n_name) ) ) );
|
|
|
|
ExprStmtNode *e2 = new(a) ExprStmtNode(pos, StmtNode::expression, new(a) BinaryExprNode(pos, ExprNode::assignment,
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, n_name),
|
|
|
|
new(a) BinaryExprNode(pos, ExprNode::subtract,
|
|
|
|
new(a) IdentifierExprNode(pos, ExprNode::identifier, n_name),
|
|
|
|
new(a) NumberExprNode(pos, 1.0) ) ) );
|
|
|
|
e1->next = e2;
|
|
|
|
BlockStmtNode *b = new(a) BlockStmtNode(pos, StmtNode::block, NULL, e1);
|
|
|
|
|
|
|
|
UnaryStmtNode *w = new(a) UnaryStmtNode(pos, StmtNode::While, c, b);
|
|
|
|
|
|
|
|
icg.genStmt(w);
|
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// return result;
|
2000-05-27 02:34:42 +04:00
|
|
|
icg.returnStmt(r_result);
|
2000-04-26 09:37:00 +04:00
|
|
|
ICodeModule *icm = icg.complete();
|
|
|
|
stdOut << icg;
|
|
|
|
|
|
|
|
// preset the global property "fact" to contain the above function
|
|
|
|
StringAtom& fact = world.identifiers[widenCString("fact")];
|
|
|
|
glob.defineFunction(fact, icm);
|
|
|
|
|
|
|
|
// now a script :
|
|
|
|
// return fact(n);
|
2000-05-27 02:34:42 +04:00
|
|
|
ICodeGenerator script(&world);
|
2000-04-26 09:37:00 +04:00
|
|
|
RegisterList args(1);
|
|
|
|
args[0] = script.loadImmediate(n);
|
2000-05-27 02:34:42 +04:00
|
|
|
script.returnStmt(script.call(script.loadName(fact), args));
|
2000-04-26 09:37:00 +04:00
|
|
|
stdOut << script;
|
|
|
|
|
|
|
|
// install a listener so we can trace execution of factorial.
|
|
|
|
Tracer t;
|
|
|
|
cx.addListener(&t);
|
|
|
|
|
|
|
|
// test the iCode interpreter.
|
|
|
|
JSValue result = cx.interpret(script.complete(), JSValues());
|
|
|
|
stdOut << "fact(" << n << ") = " << result.f64 << "\n";
|
|
|
|
|
|
|
|
delete icm;
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
return result.f64;
|
|
|
|
}
|
|
|
|
|
2000-04-28 05:43:03 +04:00
|
|
|
|
2000-05-06 07:31:35 +04:00
|
|
|
|
2000-04-21 04:04:14 +04:00
|
|
|
} /* namespace Shell */
|
|
|
|
} /* namespace JavaScript */
|
2000-04-07 06:58:01 +04:00
|
|
|
|
2000-04-26 09:37:00 +04:00
|
|
|
|
2000-04-25 06:58:30 +04:00
|
|
|
int main(int argc, char **argv)
|
2000-01-11 00:22:43 +03:00
|
|
|
{
|
2000-05-06 07:31:35 +04:00
|
|
|
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
2000-01-11 00:22:43 +03:00
|
|
|
initConsole("\pJavaScript Shell", "Welcome to the js2 shell.\n", argc, argv);
|
2000-05-06 07:31:35 +04:00
|
|
|
#endif
|
2000-04-29 04:29:56 +04:00
|
|
|
using namespace JavaScript;
|
|
|
|
using namespace Shell;
|
2000-05-12 09:15:52 +04:00
|
|
|
#if 0
|
2000-04-29 04:23:06 +04:00
|
|
|
assert(testFactorial(world, 5) == 120);
|
2000-05-12 09:15:52 +04:00
|
|
|
#endif
|
2000-04-29 04:23:06 +04:00
|
|
|
readEvalPrint(stdin, world);
|
2000-04-05 01:38:25 +04:00
|
|
|
return 0;
|
2000-04-21 04:04:14 +04:00
|
|
|
// return ProcessArgs(argv + 1, argc - 1);
|
2000-01-11 00:22:43 +03:00
|
|
|
}
|