def code_interpret(self, codeinput): global state global lexer if codeinput != self.layout.children[0]: state = State() lexer.state = state for ci in reversed(self.layout.children): if isinstance(ci, CodeInput) and ci != self.layout.children[0]: self.reinterpret_line(ci) ci._trigger_refresh_text() if hasattr(codeinput, 'out'): self.open_output(codeinput.out) return if len(codeinput.text.strip()) == 0: return res, err = state.parse(codeinput.text) self.make_output(codeinput, res, err) self.add_new_codeline()
def test_add(self): state = State() _, _ = state.parse('a=[1,0;0,-1]') _, _ = state.parse('b=[-1,0;0,1]') res, err = state.parse('a+b') self.assertFalse(err) self.assertTrue(np.all(np.equal(res, np.zeros(2))))
def test_map(self): state = State() _, _ = state.parse('a=[1,2,3]') _, _ = state.parse('f = x => x^2') res, err = state.parse('map(f, a)') self.assertFalse(err) a = np.array([1, 4, 9]) self.assertTrue(np.all(np.equal(res, a)))
def test_assign(self): state = State() res, err = state.parse('a=1') self.assertFalse(err) self.assertEqual(res, 1) res, err = state.parse('a') self.assertFalse(err) self.assertEqual(res, 1)
def test_transpose(self): state = State() _, _ = state.parse('a=[1,2;3,4]') res, err = state.parse('a\'') self.assertFalse(err) a = np.array([[1, 2], [3, 4]]) a = np.transpose(a) self.assertTrue(np.all(np.equal(a, res)))
def test_mul(self): res, err = State().parse('2*2*2') self.assertFalse(err) self.assertEqual(res, 8) res, err = State().parse('8/2/2') self.assertFalse(err) self.assertEqual(res, 2) res, err = State().parse('3 % 2') self.assertFalse(err) self.assertEqual(res, 1)
def new_script(self): global state global lexer state = State() lexer.state = state self.main_panel.layout.clear_widgets() self.main_panel.add_new_codeline()
def test_fn_closure(self): state = State() _, _ = state.parse('a=3') _, _ = state.parse('f = x => x + a') res, err = state.parse('f(20)') self.assertFalse(err) self.assertEqual(res, 23) _, _ = state.parse('a=5') res, err = state.parse('f(20)') self.assertFalse(err) self.assertEqual(res, 25)
def test_mul(self): state = State() _, _ = state.parse('a=eye(2)') _, _ = state.parse('b=[2,2;2,2]') res, err = state.parse('a*b') self.assertFalse(err) self.assertTrue(np.all(np.equal(res, np.full(2, 2)))) res, err = state.parse('a.*b') self.assertFalse(err) self.assertTrue(np.all(np.equal(res, np.eye(2) * 2)))
def test_assign(self): res, err = State().parse('a=[1,0;0,-1]') self.assertFalse(err) a = np.eye(2) a[1, 1] = -1 self.assertTrue(np.all(np.equal(a, res)))
def test_abs(self): res, err = State().parse('|-2|') self.assertFalse(err) self.assertEqual(res, 2)
def test_pow(self): res, err = State().parse('2^2^2') self.assertFalse(err) self.assertEqual(res, 16)
def test_add(self): res, err = State().parse('1-1+1') self.assertFalse(err) self.assertEqual(res, 1)
from numpy import ndarray from pygments.styles import get_style_by_name from kivy.clock import Clock from lexer import PycalcLexer, PycalcStyle from newlang import State from kivy.uix.popup import Popup from matplotlib.figure import Figure from kivy.storage.jsonstore import JsonStore from functools import partial Builder.load_file('./main.kv') state = State() lexer = PycalcLexer() lexer.state = state style = PycalcStyle FONTS = [{ "name": "VeraMono", "fn_regular": "fonts/VeraMono.ttf", "fn_bold": "fonts/VeraMono-Bold.ttf", "fn_italic": "fonts/VeraMono-Italic.ttf", "fn_bolditalic": "fonts/VeraMono-Bold-Italic.ttf" }] for font in FONTS: LabelBase.register(**font)
def test_numpy_linalg(self): res, err = State().parse('rank([1,0;0,1])') self.assertFalse(err) self.assertEqual(res, 2)
def test_numpy(self): res, err = State().parse('sin(pi/2)') self.assertFalse(err) self.assertEqual(res, 1)
def test_scalar_product(self): state = State() _, _ = state.parse('a=[2,2,2]') res, err = state.parse('a*a') self.assertFalse(err) self.assertEqual(res, 12)
def test_index(self): state = State() _, _ = state.parse('a=[1,0;0,-1]') res, err = state.parse('a(1,1)') self.assertFalse(err) self.assertEqual(res, -1)
def test_math_e(self): res, err = State().parse('1e-6') self.assertFalse(err) self.assertEqual(res, 1e-6)
def test_cond_equals(self): res, err = State().parse('1==1') self.assertFalse(err) self.assertEqual(res, True)
from newlang import State, NoOutput state = State() while True: s = input('> ') if s.startswith('/'): if s == '/exit': break elif s == '/vars': for k, v in state.ctx.var_map.items(): print(f'{k} =') print(v) print() elif s.startswith('/clear'): args = s.split(' ') args.pop(0) if args: for name in args: if name in state.ctx.var_map: del state.ctx.var_map[name] print(f'Cleared vars: {", ".join(args)}') else: state.ctx.clear() print("Cleared all vars") else: print(f"Unknown command: {s}") else: