[pypy-svn] r65119 - in pypy/branch/js-refactoring/pypy: lang/js translator/goal
jandem at codespeak.net
jandem at codespeak.net
Wed May 6 20:43:45 CEST 2009
Author: jandem
Date: Wed May 6 20:43:44 2009
New Revision: 65119
Added:
pypy/branch/js-refactoring/pypy/lang/js/console.py
Modified:
pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
pypy/branch/js-refactoring/pypy/translator/goal/targetjsstandalone.py
Log:
Add translatable, interactive console. Work-in-progress, but the basics work.
Added: pypy/branch/js-refactoring/pypy/lang/js/console.py
==============================================================================
--- (empty file)
+++ pypy/branch/js-refactoring/pypy/lang/js/console.py Wed May 6 20:43:44 2009
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+
+import autopath
+import os, sys
+from pypy.lang.js.interpreter import load_source, Interpreter, load_file
+from pypy.lang.js.jsparser import parse, ParseError
+from pypy.lang.js.jsobj import W_Builtin, W_String, ThrowException, w_Undefined
+from pypy.rlib.streamio import open_file_as_stream
+
+def printmessage(msg):
+ if msg is None:
+ return
+ os.write(1, msg)
+
+def readline():
+ result = []
+ while 1:
+ s = os.read(0, 1)
+ result.append(s)
+ if s == '\n':
+ break
+
+ if s == '':
+ if len(result) > 1:
+ break
+ raise SystemExit
+ return ''.join(result)
+
+class JSConsole(object):
+ prompt_ok = 'js> '
+ prompt_more = '... '
+
+ def __init__(self):
+ self.interpreter = Interpreter()
+
+ def runsource(self, source, filename='<input>'):
+ try:
+ ast = load_source(source, filename)
+ except ParseError, exc:
+ if exc.source_pos.i == len(source):
+ # more input needed
+ return True
+ else:
+ # syntax error
+ self.showsyntaxerror(filename, exc)
+ return False
+
+ # execute it
+ self.runcode(ast)
+ return False
+
+ def runcode(self, ast):
+ """Run the javascript code in the AST. All exceptions raised
+ by javascript code must be caught and handled here. When an
+ exception occurs, self.showtraceback() is called to display a
+ traceback.
+ """
+ try:
+ res = self.interpreter.run(ast, interactive=True)
+ if res is not None and res != w_Undefined:
+ try:
+ printmessage(res.ToString(self.interpreter.global_context))
+ except ThrowException, exc:
+ printmessage(exc.exception.ToString(self.interpreter.global_context))
+ printmessage('\n')
+ except SystemExit:
+ raise
+ #except ThrowException, exc:
+ # self.showtraceback(exc)
+
+ def showsyntaxerror(self, filename, exc):
+ pass
+
+ def interact(self):
+ printmessage('PyPy JavaScript Interpreter\n')
+ printmessage(self.prompt_ok)
+
+ lines = []
+
+ while True:
+ try:
+ line = readline()
+ except SystemExit, e:
+ printmessage('\n')
+ return
+
+ lines.append(line)
+
+ source = ''.join(lines)
+ need_more = self.runsource(source)
+
+ if need_more:
+ printmessage(self.prompt_more)
+ else:
+ printmessage(self.prompt_ok)
+ lines = []
+
+if __name__ == '__main__':
+ console = JSConsole()
+ console.interact()
Modified: pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/interpreter.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/interpreter.py Wed May 6 20:43:44 2009
@@ -725,8 +725,9 @@
w_math.Put(ctx, 'E', W_FloatNumber(math.e), flags=allon)
w_math.Put(ctx, 'LN2', W_FloatNumber(math.log(2)), flags=allon)
w_math.Put(ctx, 'LN10', W_FloatNumber(math.log(10)), flags=allon)
- #w_math.Put(ctx, 'LOG2E', W_FloatNumber(math.log(math.e, 2)), flags=allon)
- #w_math.Put(ctx, 'LOG10E', W_FloatNumber(math.log(math.e, 10)), flags=allon)
+ log2e = math.log(math.e) / math.log(2) # rpython supports log with one argument only
+ w_math.Put(ctx, 'LOG2E', W_FloatNumber(log2e), flags=allon)
+ w_math.Put(ctx, 'LOG10E', W_FloatNumber(math.log10(math.e)), flags=allon)
w_math.Put(ctx, 'PI', W_FloatNumber(math.pi), flags=allon)
w_math.Put(ctx, 'SQRT1_2', W_FloatNumber(math.sqrt(0.5)), flags=allon)
w_math.Put(ctx, 'SQRT2', W_FloatNumber(math.sqrt(2)), flags=allon)
Modified: pypy/branch/js-refactoring/pypy/translator/goal/targetjsstandalone.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/translator/goal/targetjsstandalone.py (original)
+++ pypy/branch/js-refactoring/pypy/translator/goal/targetjsstandalone.py Wed May 6 20:43:44 2009
@@ -6,6 +6,7 @@
import sys
from pypy.lang.js.interpreter import *
+from pypy.lang.js.console import JSConsole
# __________ Entry point __________
@@ -15,6 +16,12 @@
def entry_point(argv):
i = 1
+
+ if len(argv) == 1:
+ console = JSConsole()
+ console.interact()
+ return 0
+
interp = Interpreter()
while i < len(argv):
arg = argv[i]
More information about the Pypy-commit
mailing list