Ejemplo n.º 1
0
class ParserTest(unittest.TestCase):
  def setUp(self):
    self.lexer = Lexer()
    self.parser = Parser()

  def test_call(self):
    """Ensures function calls are parsed properly."""
    code = "print('hello')"
    tokens = self.lexer.parse(code)
    parsed = self.parser.parse_literal(tokens)
    assert parsed == "[{CallExpression {IdentLiteral print} [{StringLiteral hello}]}]", "Issue in function call parsing."

  def test_math(self):
    """Ensures arithmetic is parsed properly."""
    code = "5 + 5 / 2"
    tokens = self.lexer.parse(code)
    parsed = self.parser.parse_literal(tokens)
    assert parsed == "[{BinaryOperator + {left: {IntegerLiteral 5}, right: {BinaryOperator / {left: {IntegerLiteral 5}, right: {IntegerLiteral 2}}}}}]", "Issue in arithmetic parsing."
  
  def test_assignment_1(self):
    """Ensures variable declaration and assignment are parsed properly."""
    code = "int test = 5 * 5 / 5"
    tokens = self.lexer.parse(code)
    parsed = self.parser.parse_literal(tokens)
    assert parsed == "[{Assignment {IdentLiteral test} {BinaryOperator / {left: {BinaryOperator * {left: {IntegerLiteral 5}, right: {IntegerLiteral 5}}}, right: {IntegerLiteral 5}}}}]", "Issue in assignment parsing."
  
  def test_conditional_1(self):
    """Ensures conditionals are parsed properly."""
    code = "if (1 == 1) return 1"
    tokens = self.lexer.parse(code)
    parsed = self.parser.parse_literal(tokens)
    assert parsed == "[{Conditional if {BinaryOperator == {left: {IntegerLiteral 1}, right: {IntegerLiteral 1}}} [{Return {IntegerLiteral 1}}] []}]", "Issue in inline conditional parsing."
Ejemplo n.º 2
0
class ParserTest(unittest.TestCase):
    def setUp(self):
        self.lexer = Lexer()
        self.parser = Parser()

    def test_call(self):
        """Ensures function calls are parsed properly."""
        code = "print('hello')"
        tokens = self.lexer.parse(code)
        parsed = self.parser.parse_literal(tokens)[0]
        test = CallExpression(name=IdentLiteral(name='print'),
                              args=[StringLiteral('hello')])
        assert pickle(parsed) == pickle(
            test), "Issue in function call parsing."

    def test_math(self):
        """Ensures arithmetic is parsed properly."""
        code = "5 + 5 / 2"
        tokens = self.lexer.parse(code)
        parsed = self.parser.parse_literal(tokens)[0]
        test = BinaryOperator(operator='+',
                              left=IntegerLiteral(5),
                              right=BinaryOperator(operator='/',
                                                   left=IntegerLiteral(5),
                                                   right=IntegerLiteral(2)))
        assert pickle(parsed) == pickle(test), "Issue in arithmetic parsing."

    def test_assignment_1(self):
        """Ensures variable declaration and assignment are parsed properly."""
        code = "let test = 5 * 5 / 5"
        tokens = self.lexer.parse(code)
        parsed = self.parser.parse_literal(tokens)[0]
        test = Assignment(_type='let',
                          ident=IdentLiteral(name='test'),
                          value=BinaryOperator(operator='/',
                                               left=BinaryOperator(
                                                   operator='*',
                                                   left=IntegerLiteral(5),
                                                   right=IntegerLiteral(5)),
                                               right=IntegerLiteral(5)))
        assert pickle(parsed) == pickle(test), "Issue in assignment parsing."

    def test_conditional_1(self):
        """Ensures conditionals are parsed properly."""
        code = "if (1 == 1) return 1"
        tokens = self.lexer.parse(code)
        parsed = self.parser.parse_literal(tokens)[0]
        test = Conditional('if',
                           expression=BinaryOperator(operator='==',
                                                     left=IntegerLiteral(1),
                                                     right=IntegerLiteral(1)),
                           body=[Return(IntegerLiteral(1))],
                           else_body=[])
        assert pickle(parsed) == pickle(
            test), "Issue in inline conditional parsing."
Ejemplo n.º 3
0
 def __init__(self,
              stdin,
              stdout,
              environment=Environment(),
              lexer=Lexer(),
              parser=Parser(),
              interpreter=Interpreter()):
     self.stdin = stdin
     self.stdout = stdout
     self.env = environment
     self.lexer = lexer
     self.parser = parser
     self.interpreter = interpreter
Ejemplo n.º 4
0
class InterpreterTest(unittest.TestCase):
    def setUp(self):
        self.lexer = Lexer()
        self.parser = Parser()
        self.interpreter = Interpreter()
        self.env = Environment()

    def test_math(self):
        """Ensures arithmetic is evaluated properly."""
        code = "4 + 2 / 2"
        tokens = self.lexer.parse(code)
        parsed = optimize(self.parser.parse(tokens))
        evaluated = self.interpreter.evaluate(parsed, self.env)[0]
        assert evaluated == 5, "Issue in arithmetic evaluation."
Ejemplo n.º 5
0
 def setUp(self):
   self.lexer = Lexer()
   self.parser = Parser()
Ejemplo n.º 6
0
import sys
from jink.lexer import Lexer
from jink.parser import Parser
from jink.optimizer import optimize
from jink.interpreter import Interpreter
from jink.utils.classes import Environment
lexer = Lexer()
parser = Parser()
interpreter = Interpreter()

code = ''
if len(sys.argv) > 1:
  if not sys.argv[1].endswith('.jk'):
    sys.argv[1] += '.jk'
  code = open(sys.argv[1]).read()
else:
  raise Exception("File path not given.")

if not code:
  raise Exception("Error in file reading.")

env = Environment()
env.def_func('print', lambda scope, args: print('\n'.join([str(x) for x in args]) or 'null'))
env.def_func('string', lambda args: [str(x or 'null') for x in args][0] if len(args) == 1 else [str(x or 'null') for x in args])
AST = optimize(parser.parse(lexer.parse(code)))
interpreter.evaluate(AST, env)
Ejemplo n.º 7
0
 def setUp(self):
     self.lexer = Lexer()
     self.parser = Parser()
     self.interpreter = Interpreter()
     self.env = Environment()
Ejemplo n.º 8
0
      "",
      "args:",
      "  > -v -- verbose; will output AST." # and if compiling, both optimized and unoptimized LLVM IR.",
      # "  > -c -- compile; will use compiler instead of interpreter."
      "",
      "usage:",
      "  > [jink] help                 -- shows this prompt.",
      "  > [jink] path/to/file[.jk]    -- executes interpreter on file.",
      "  > [jink] -v path/to/file[.jk] -- executes interpreter on file verbose mode.",
      # "  > [jink] -c path/to/file[.jk] -- executes compiler on file.",
      # "  > [jink] -c -v path/to/file[.jk] -- executes compiler on file in verbose mode.",
      "  > [jink]                      -- launches interpreted interactive REPL.",
      "  > [jink] -v                   -- launches interpreted interactive REPL in verbose mode."
    ]))

  else:
    path = ' '.join(sys.argv)
    code = get_code_from_path(path)

    if to_compile:
      raise NotImplementedError("Compiler not yet implemented.")
      # Compiler()._eval(code, optimize=True, verbose=verbose)
    else:
      AST = optimize(Parser().parse(Lexer().parse(code), verbose=verbose))
      env = Environment()
      env.add_builtins()
      Interpreter().evaluate(AST, env)

if __name__ == "__main__":
  pass