From 496dfb7026b6e635d93b68d90c447e92eab14f4f Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Tue, 14 Jun 2022 01:19:30 -0600 Subject: [PATCH] Calling it a night --- .../shoggoth/src/python/ichor/__main__.py | 7 ++-- projects/shoggoth/src/python/ichor/state.py | 39 +++++++++++++++++++ .../test/python/ichor/test_parsers.py | 25 ++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 projects/shoggoth/test/python/ichor/test_parsers.py diff --git a/projects/shoggoth/src/python/ichor/__main__.py b/projects/shoggoth/src/python/ichor/__main__.py index 8ba9303..db61d41 100644 --- a/projects/shoggoth/src/python/ichor/__main__.py +++ b/projects/shoggoth/src/python/ichor/__main__.py @@ -4,20 +4,21 @@ ichor entrypoint """ -from . import * +from . import Opcode, Interpreter, BOOTSTRAP, XOR3 def main(): vm = Interpreter(BOOTSTRAP) ret = vm.run( [ - Opcode.FUNREF(XOR3), + Opcode.IDENTIFIERC(XOR3), + Opcode.FUNREF(), Opcode.CLOSUREF(1), Opcode.CLOSUREC(1), Opcode.CALLC(1), Opcode.RETURN(1), ], - stackframe = [True, True, False] + stack = [True, True, False] ) print(ret) diff --git a/projects/shoggoth/src/python/ichor/state.py b/projects/shoggoth/src/python/ichor/state.py index deb30a9..ba5b006 100644 --- a/projects/shoggoth/src/python/ichor/state.py +++ b/projects/shoggoth/src/python/ichor/state.py @@ -6,6 +6,7 @@ import typing as t from ichor.isa import Opcode +from lark import Lark class Identifier(t.NamedTuple): @@ -34,6 +35,28 @@ class FunctionSignature(t.NamedTuple): cls.parse_list(ret) ) +GRAMMAR = r""" +fun: constraints ";" name ";" arguments ";" ret + +arguments: (type ","?)* +ret: (type ","?)* +constraints: (constraint ","?)* +constraint: type + +var: constraints ";" name ";" arms +arms: (arm ("," arms)?)? +arm: name "(" bindings ")" + +bindings: (binding ","?)* +binding: name ":" type + +type: NAME +name: NAME +NAME: /[^;,:⊢]+/ +""" + +FUNC = Lark(GRAMMAR, start="fun") + class FunctionRef(t.NamedTuple): raw: str @@ -60,6 +83,22 @@ class FunctionRef(t.NamedTuple): class Function(t.NamedTuple): name: str + arguments: t.List[str] + returns: t.List[str] + instructions: t.List[Opcode] + typevars: t.List[t.Any] = [] + typeconstraints: t.List[t.Any] = [] + metadata: dict = {} + + +VAR = Lark(GRAMMAR, start="var") + +class Type(t.NamedTuple): + name: str + constructors: t.List[t.Tuple[str, t.List[str]]] + typevars: t.List[t.Any] = [] + typeconstraints: t.List[t.Any] = [] + metadata: dict = {} class Closure(t.NamedTuple): diff --git a/projects/shoggoth/test/python/ichor/test_parsers.py b/projects/shoggoth/test/python/ichor/test_parsers.py new file mode 100644 index 0000000..60ce168 --- /dev/null +++ b/projects/shoggoth/test/python/ichor/test_parsers.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +from ichor.state import FUNC, VAR + +import pytest + + +@pytest.mark.parametrize('sig', [ + ";not;bool;bool", + ";and;bool,bool;bool", + ";or;bool,bool,bool;bool", +]) +def test_func_parses(sig): + assert FUNC.parse(sig) + + +@pytest.mark.parametrize('sig', [ + ";bool;true(),false()", + "A,B;pair;pair(a:A,b:B)", + "A,B,C;tripple;tripple(a:A,b:B,c:C)", + "A,B,C,D;quad;quad(a:A,b:B,c:C,d:D)", + "A,B,C,D,E;quint;quint(a:A,b:B,c:C,d:D,e:E)", +]) +def test_var_parses(sig): + assert VAR.parse(sig)