diff --git a/compile.py b/compile.py index 5966939..deed559 100644 --- a/compile.py +++ b/compile.py @@ -2,9 +2,9 @@ from emis_funky_funktions import * from typing import Collection, Sequence, TypeAlias from ir import BUILTIN_SUBSTITUTIONS, Expression, ReplHole, subst_all -from genir import json_to_ir, PatternParseProblem, BranchTypesDiffer, UndefinedVariable -from types_ import BUILTINS_CONTEXT, UnificationError -from silly_thing import evaluate +from ir.genir import json_to_ir, PatternParseProblem, BranchTypesDiffer, UndefinedVariable +from ir.types_ import BUILTINS_CONTEXT, UnificationError +from repl import evaluate from opt import optimize_to_fixpoint, all_optimizations import json diff --git a/grammar.py b/grammar.py deleted file mode 100644 index e3128f7..0000000 --- a/grammar.py +++ /dev/null @@ -1,39 +0,0 @@ -from enum import auto, IntEnum -from typing import Collection, Tuple -from re import compile, Pattern - -class Tok(IntEnum): - """ - All possible tokens used in the grammar - """ - Whitespace = auto() - OpenCurly = auto() - CloseCurly = auto() - OpenSquare = auto() - CloseSquare = auto() - Comma = auto() - Colon = auto() - String = auto() - Number = auto() - Eof = auto() - - def __repr__(self): - return self._name_ - -LEX_TABLE: Collection[Tuple[Pattern[str], Tok]] = [ - (compile(r"[\s\n]+"), Tok.Whitespace), - (compile(r"{"), Tok.OpenCurly), - (compile(r"}"), Tok.CloseCurly), - (compile(r"\["), Tok.OpenSquare), - (compile(r"\]"), Tok.CloseSquare), - (compile(r","), Tok.Comma), - (compile(r":"), Tok.Colon), - (compile(r'"[^"]*"'), Tok.String), - (compile(r'\d+'), Tok.Number), -] -""" -A mapping of regexs to the tokens the identify - -Tokens earlier on in the list should be regarded as higher priority, even if a match lower -on the list also matches. All unicode strings should be matched by at least one token. -""" \ No newline at end of file diff --git a/ir.py b/ir/__init__.py similarity index 98% rename from ir.py rename to ir/__init__.py index 072e5ef..dcddc9f 100644 --- a/ir.py +++ b/ir/__init__.py @@ -2,10 +2,10 @@ from emis_funky_funktions import * from typing import Collection, Mapping, Sequence, Tuple, TypeAlias from functools import reduce -from match_tree import MatchTree, MatchException, StructurePath, LeafNode, merge_all_trees, IntNode, EMPTY_STRUCT_PATH, FAIL_NODE -from patterns import Pattern +from ir.match_tree import MatchTree, MatchException, StructurePath, LeafNode, merge_all_trees, IntNode, EMPTY_STRUCT_PATH, FAIL_NODE +from ir.patterns import Pattern -import types_ +from ir import types_ Expression: TypeAlias = 'MonoFunc | Application | Int | Variable | Builtin | LetBinding | ReplHole | Switch' diff --git a/genir.py b/ir/genir.py similarity index 96% rename from genir.py rename to ir/genir.py index 1f302ab..0afaadb 100644 --- a/genir.py +++ b/ir/genir.py @@ -1,11 +1,11 @@ from emis_funky_funktions import * from typing import Sequence, Mapping -from pattern import lex_and_parse_pattern +from parse.pattern import lex_and_parse_pattern from ir import Expression, MonoFunc, Application, Int, Variable, LetBinding, ReplHole -from patterns import Pattern, NamePattern, IgnorePattern, IntPattern, SPattern -from match_tree import MatchException -from types_ import * +from ir.patterns import Pattern, NamePattern, IgnorePattern, IntPattern, SPattern +from ir.match_tree import MatchException +from ir.types_ import * from functools import reduce import json diff --git a/match_tree.py b/ir/match_tree.py similarity index 100% rename from match_tree.py rename to ir/match_tree.py diff --git a/patterns.py b/ir/patterns.py similarity index 95% rename from patterns.py rename to ir/patterns.py index 5dfc271..9901c82 100644 --- a/patterns.py +++ b/ir/patterns.py @@ -2,8 +2,8 @@ from emis_funky_funktions import * from typing import Collection, Mapping, Sequence, Tuple, TypeAlias -import types_ -from match_tree import LeafNode, IntNode, MatchTree, StructurePath, FAIL_NODE, EMPTY_STRUCT_PATH +import ir.types_ +from ir.match_tree import LeafNode, IntNode, MatchTree, StructurePath, FAIL_NODE, EMPTY_STRUCT_PATH Pattern: TypeAlias = 'NamePattern | IntPattern | SPattern | IgnorePattern' diff --git a/types_.py b/ir/types_.py similarity index 100% rename from types_.py rename to ir/types_.py diff --git a/main.py b/main.py index f3aad64..8a6940d 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,8 @@ from emis_funky_funktions import * -from genir import json_to_ir -from types_ import BUILTINS_CONTEXT -from silly_thing import repl, repl_expr +from ir.genir import json_to_ir +from ir.types_ import BUILTINS_CONTEXT +from repl import repl, repl_expr import json, sys diff --git a/comb_parse.py b/parse/comb_parse.py similarity index 99% rename from comb_parse.py rename to parse/comb_parse.py index b5b1923..4e66a86 100644 --- a/comb_parse.py +++ b/parse/comb_parse.py @@ -5,7 +5,7 @@ from enum import auto, IntEnum from functools import reduce from re import compile, Pattern -from lex import Lexeme, tokenize +from parse.lex import Lexeme, tokenize from typing import Any, Callable, Collection, Mapping, Sequence, Tuple, TypeAlias diff --git a/lex.py b/parse/lex.py similarity index 98% rename from lex.py rename to parse/lex.py index 58248b7..a474405 100644 --- a/lex.py +++ b/parse/lex.py @@ -99,5 +99,4 @@ if __name__ == '__main__': # print(tokenize(open('sample.cnf').read())) import doctest from re import compile - from grammar import Tok, LEX_TABLE doctest.testmod() \ No newline at end of file diff --git a/pattern.py b/parse/pattern.py similarity index 90% rename from pattern.py rename to parse/pattern.py index 46f0608..74343b1 100644 --- a/pattern.py +++ b/parse/pattern.py @@ -1,9 +1,9 @@ from emis_funky_funktions import * from typing import Collection, Mapping, Sequence, Tuple, TypeAlias -from comb_parse import Parser -from patterns import Pattern, NamePattern, IgnorePattern, IntPattern, SPattern -from lex import Lexeme, tokenize +from parse.comb_parse import Parser +from ir.patterns import Pattern, NamePattern, IgnorePattern, IntPattern, SPattern +from parse.lex import Lexeme, tokenize from enum import auto, IntEnum import re diff --git a/parse2.py b/parse2.py deleted file mode 100644 index 092fcfd..0000000 --- a/parse2.py +++ /dev/null @@ -1,58 +0,0 @@ -from emis_funky_funktions import * - -from typing import AbstractSet, FrozenSet, TypeAlias, TypeGuard, TypeVar - -Lexeme = TypeVar('Lexeme') -Token = TypeVar('Token') -Variable = TypeVar('Variable') - -Handle: TypeAlias = Sequence[Variable | Token] -Production: TypeAlias = Tuple[Variable, Handle[Variable, Token]] -Grammar: TypeAlias = Sequence[Production[Variable, Token]] - -NfaState: TypeAlias = Tuple[int, int] -Nfa: TypeAlias = Callable[[NfaState, Variable | Token], FrozenSet[NfaState]] - -DfaState: TypeAlias = FrozenSet(Tuple[int, int]) -Dfa: TypeAlias = Callable[[DfaState, Variable | Token], FrozenSet[NfaState]] - -def build_nfa( - is_var: Callable[[Variable | Token], TypeGuard[Variable]], - grammar: Grammar[Variable, Token], -) -> Nfa[Variable, Token]: - - def epsilon_closure_step(state: NfaState) -> FrozenSet[NfaState]: - production_no, symbol_no = state - _, production = grammar[production_no] - next_symbol = production[symbol_no] - - if is_var(next_symbol): - possible_productions: Iterator[NfaState] = ((i, 0) for i, (variable, handle) in enumerate(grammar) if variable == next_symbol) - return fset(state, *possible_productions) - else: - return fset(state,) - - def epsilon_closure(states: FrozenSet[NfaState], previous_states: FrozenSet[NfaState] = fset()) -> FrozenSet[NfaState]: - new_states = FSet(new_state for old_state in states for new_state in epsilon_closure_step(old_state)) - previous_states - states - if len(new_states) == 0: - return states | previous_states - else: - return epsilon_closure(new_states, states | previous_states) - - def nfa(state: Tuple[int, int], symbol: Variable | Token) -> FrozenSet[NfaState]: - production_no, symbol_no = state - production = grammar[production_no] - next_symbol = production[symbol_no] - if next_symbol == symbol: - return epsilon_closure(fset((production_no, symbol_no + 1))) - else: - return fset() - - def dfa(dstate: DfaState, symbol: Variable | Token) -> DfaState: - return FSet( - new_nstate - for nstate in dstate - for new_nstate in nfa(nstate, symbol) - ) - - return nfa diff --git a/silly_thing.py b/repl.py similarity index 94% rename from silly_thing.py rename to repl.py index 024f80e..c7c9710 100644 --- a/silly_thing.py +++ b/repl.py @@ -2,8 +2,8 @@ from emis_funky_funktions import * from typing import Collection, Sequence, TypeAlias from ir import Expression, ReplHole, subst_all -from genir import json_to_ir, PatternParseProblem, BranchTypesDiffer, UndefinedVariable -from types_ import BUILTINS_CONTEXT, UnificationError +from ir.genir import json_to_ir, PatternParseProblem, BranchTypesDiffer, UndefinedVariable +from ir.types_ import BUILTINS_CONTEXT, UnificationError import json from dataclasses import dataclass