Skip to content

Commit

Permalink
Modulise clean (#17)
Browse files Browse the repository at this point in the history
* make all the imports relative, so all this can be imported in python, towards integration goals. 

* add a __main__.py to RASP_support, that starts the REPL, so the module can still be used directly as a REPL, now by calling python3 -m RASP_support. 

* update rasp.sh and tests/test_all.py to use this new call

* add init file to the module, exposing rasp repl for easy use after import
  • Loading branch information
gailweiss authored Mar 1, 2024
1 parent 8fbbc67 commit c2a8726
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 30 deletions.
14 changes: 11 additions & 3 deletions RASP_support/DrawCompFlow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from FunctionalSupport import Unfinished, guarded_contains, base_tokens, \
from .FunctionalSupport import Unfinished, guarded_contains, base_tokens, \
tokens_asis
from Support import clean_val
from .Support import clean_val
import os
import string
import analyse # adds useful functions to all the Unfinisheds

# adds useful functions to all the Unfinisheds
# using dummyimport to import analyse with the relative "." because python
# won't let me do "import .analyse"
from .analyse import dummyimport


# fix: in ordering, we always connect bottom FF to top select. but sometimes,
# there is no FF (if go straight into next select), or there is no rendered
Expand Down Expand Up @@ -602,3 +607,6 @@ def draw_comp_flow(self, w, filename=None,
g.view()
if not keep_dot:
os.remove(filename)


dummyimport = None
4 changes: 2 additions & 2 deletions RASP_support/Environment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from FunctionalSupport import Unfinished, RASPTypeError, tokens_asis, \
from .FunctionalSupport import Unfinished, RASPTypeError, tokens_asis, \
tokens_str, tokens_int, tokens_bool, tokens_float, indices
from Evaluator import RASPFunction
from .Evaluator import RASPFunction


class UndefinedVariable(Exception):
Expand Down
8 changes: 4 additions & 4 deletions RASP_support/Evaluator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from FunctionalSupport import select, zipmap, aggregate, \
from .FunctionalSupport import select, zipmap, aggregate, \
or_selects, and_selects, not_select, indices, \
Unfinished, UnfinishedSequence, UnfinishedSelect
from Sugar import tplor, tpland, tplnot, toseq, full_s
from Support import RASPTypeError, RASPError
from .Sugar import tplor, tpland, tplnot, toseq, full_s
from .Support import RASPTypeError, RASPError
from collections.abc import Iterable
from zzantlr.RASPParser import RASPParser
from .zzantlr.RASPParser import RASPParser

ENCODER_NAME = "s-op"

Expand Down
8 changes: 4 additions & 4 deletions RASP_support/FunctionalSupport.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from Support import aggregate as _aggregate
from Support import Sequence, RASPTypeError
from Support import select as _select
from Support import zipmap as _zipmap
from .Support import aggregate as _aggregate
from .Support import Sequence, RASPTypeError
from .Support import select as _select
from .Support import zipmap as _zipmap
import traceback
import sys # for readable exception handling
from collections.abc import Iterable
Expand Down
12 changes: 6 additions & 6 deletions RASP_support/REPL.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from antlr4.error.ErrorListener import ErrorListener
from antlr4 import CommonTokenStream, InputStream
from collections.abc import Iterable
from zzantlr.RASPLexer import RASPLexer
from zzantlr.RASPParser import RASPParser
from Environment import Environment, UndefinedVariable, ReservedName
from FunctionalSupport import UnfinishedSequence, UnfinishedSelect, Unfinished
from Evaluator import Evaluator, NamedVal, NamedValList, JustVal, \
from .zzantlr.RASPLexer import RASPLexer
from .zzantlr.RASPParser import RASPParser
from .Environment import Environment, UndefinedVariable, ReservedName
from .FunctionalSupport import UnfinishedSequence, UnfinishedSelect, Unfinished
from .Evaluator import Evaluator, NamedVal, NamedValList, JustVal, \
RASPFunction, ArgsError, RASPTypeError, RASPValueError
from Support import Select, Sequence, lazy_type_check
from .Support import Select, Sequence, lazy_type_check

ENCODER_NAME = "s-op"

Expand Down
14 changes: 8 additions & 6 deletions RASP_support/Sugar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from FunctionalSupport import Unfinished as _Unfinished
from FunctionalSupport import UnfinishedSequence as _UnfinishedSequence
from FunctionalSupport import select, zipmap
from make_operators import add_ops
import DrawCompFlow
from .FunctionalSupport import Unfinished as _Unfinished
from .FunctionalSupport import UnfinishedSequence as _UnfinishedSequence
from .FunctionalSupport import select, zipmap
from .make_operators import add_ops
from .DrawCompFlow import dummyimport
# DrawCompFlow is not at all necessary for sugar, but sugar is really the
# top-level rasp file we import, and nice to have draw_comp_flow added into
# the sequences already on load
# the sequences already on load. also, don't *really* need to import
# dummy specifically, but python won't accept "import .DrawCompFlow"
# while it will accept some form of "from .DrawCompFlow import ..."


def _apply_unary_op(self, f):
Expand Down
1 change: 1 addition & 0 deletions RASP_support/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .REPL import REPL
3 changes: 3 additions & 0 deletions RASP_support/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .REPL import REPL

REPL().run()
5 changes: 4 additions & 1 deletion RASP_support/analyse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from FunctionalSupport import Unfinished, UnfinishedSequence, \
from .FunctionalSupport import Unfinished, UnfinishedSequence, \
UnfinishedSelect, guarded_contains, guarded_compare, zipmap
from collections import defaultdict, Counter
from copy import copy
Expand Down Expand Up @@ -353,3 +353,6 @@ def pre_aggregate_comp(seq):
if isinstance(vreal, tuple): # equivalently, if seq.output_index >= 0:
vreal = vreal[seq.output_index]
return vreal


dummyimport = None
4 changes: 2 additions & 2 deletions rasp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ source raspenv/bin/activate

if [[ $(rlwrap -v) == rlwrap* ]]; then
# the better option. requires rlwrap
rlwrap python3 RASP_support/REPL.py
rlwrap python3 -m RASP_support
else
python3 RASP_support/REPL.py
python3 -m RASP_support
fi

deactivate
4 changes: 2 additions & 2 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def check_equal(f1, f2):


def run_input(name):
os.system("python3 RASP_support/REPL.py <" +
os.system("python3 -m RASP_support <" +
joinpath(inpath, name)+" >"+joinpath(outpath, name))
fix_file_paths(joinpath(outpath, name), curr_path_marker)
return check_equal(joinpath(outpath, name), joinpath(tgtpath, name))
Expand All @@ -34,7 +34,7 @@ def run_inputs():

def test_broken_lib(lib):
os.system("cp "+joinpath(libspath, lib)+" RASP_support/rasplib.rasp")
os.system("python3 RASP_support/REPL.py <"+joinpath(libtestspath,
os.system("python3 -m RASP_support <"+joinpath(libtestspath,
"empty.txt") + " >"+joinpath(liboutspath, lib))
return check_equal(joinpath(liboutspath, lib), joinpath(libtgtspath, lib))

Expand Down

0 comments on commit c2a8726

Please sign in to comment.