-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproduction.py
39 lines (28 loc) · 1.54 KB
/
production.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from typing import List, Type, Optional, Callable, Any
from lib.interpreter.pattern import Pattern, Predicate, StrFunc, Tag
from lib.lang.symbol import NonterminalSymbol, Symbol
"""
basic production class
since we implement the dsl separate, this class also connects the grammar with execution (i.e. the Pattern class)
"""
class Production:
"""
General production class
"""
def __init__(self, function_name: str, ret_sym: NonterminalSymbol, arg_syms: List[Symbol] | List[List[Symbol]],
constructor: Optional[Type[Pattern] | Type[Predicate] | Type[StrFunc] | Callable[[Any], Predicate | Pattern | StrFunc]]):
self.function_name: str = function_name
self.ret_sym: NonterminalSymbol = ret_sym
self.arg_syms: List[Symbol] = arg_syms
# take in a pattern constructor (or a callable function that returns a pattern
self.constructor: Optional[Type[Pattern] | Callable[[Any], Predicate | Pattern] | StrFunc] = constructor
def __repr__(self):
return '{}({}) -> {}'.format(self.function_name, ','.join([repr(s) for s in self.arg_syms]), self.ret_sym)
class IdProduction(Production):
"""
Production of the pattern ret_sym := arg_sym
# TODO: the constructor is really overloaded
"""
def __init__(self, function_name: str, ret_sym: NonterminalSymbol, arg_syms: List[Symbol],
constructor: Optional[Type[Pattern | Tag | StrFunc] | StrFunc | Callable[[Any], Predicate | Pattern | StrFunc]]):
super().__init__(function_name, ret_sym, arg_syms, constructor)