Skip to content

Commit

Permalink
Start moving things to shell_ast folder
Browse files Browse the repository at this point in the history
  • Loading branch information
angelhof committed Feb 21, 2023
1 parent 4580b03 commit 55966bb
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 45 deletions.
34 changes: 4 additions & 30 deletions compiler/ast_to_ir.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ast_util import *
from shell_ast.ast_util import *
from ir import *
from definitions.ast_node import *
from definitions.ast_node_c import *
Expand All @@ -8,6 +8,9 @@
import subprocess
import config

## TODO: Separate the ir stuff to the bare minimum and
## try to move this to the shell_ast folder.

##
## Compile AST -> Extended AST with IRs
##
Expand Down Expand Up @@ -291,35 +294,6 @@ def should_expand_arg_char(arg_char):
def should_expand_argument(argument):
return any([should_expand_arg_char(arg_char) for arg_char in argument])

def make_echo_ast(argument, var_file_path):
nodes = []
## Source variables if present
if(not var_file_path is None):
arguments = [string_to_argument("source"), string_to_argument(var_file_path)]

line_number = 0
node = make_kv('Command', [line_number, [], arguments, []])
nodes.append(node)

## Reset the exit status
variable_arg = make_kv('V', ['Normal', "false", 'pash_previous_exit_status', []])
arguments = [string_to_argument("exit"), [variable_arg]]
exit_node = make_kv('Command', [0, [], arguments, []])
node = make_kv('Subshell', [0, exit_node, []])
nodes.append(node)

## Reset the input arguments
variable_arg = make_kv('V', ['Normal', "false", 'pash_input_args', []])
arguments = [string_to_argument("set"), string_to_argument("--"), [variable_arg]]
set_node = make_kv('Command', [0, [], arguments, []])
nodes.append(set_node)

arguments = [string_to_argument("echo"), string_to_argument("-n"), argument]

line_number = 0
node = make_kv('Command', [line_number, [], arguments, []])
nodes.append(node)
return nodes

## TODO: Move this function somewhere more general
def execute_shell_asts(asts):
Expand Down
10 changes: 2 additions & 8 deletions compiler/ir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,8 @@ def format_expanded_arg_char(arg_char):
## TODO: Make this correct
raise ValueError

## These functions check tuple inputs (configuration and streaming ones)
def is_single_input(inputs):
assert(False)
assert(isinstance(inputs, tuple))
conf_inputs = inputs[0]
streaming_inputs = inputs[1]
return (len(conf_inputs) == 0
and len(streaming_inputs) == 1)

## TODO: This seems like it should go to ast_util

## This function gets a key and a value from the ast json format
def get_kv(dic):
Expand Down
6 changes: 2 additions & 4 deletions compiler/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import subprocess
import sys

from ast_util import *
from shell_ast.ast_util import UnparsedScript
from util import *
from definitions.ast_node import *

sys.path.append(os.path.join(config.PASH_TOP, "compiler/parser/ceda"))
from definitions.ast_node import AstNode, ast_node_to_untyped_deep

import libdash.parser
import libdash.printer
Expand Down
4 changes: 3 additions & 1 deletion compiler/pash.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import subprocess
import argparse
from datetime import datetime
import ast_to_ast

from shell_ast import ast_to_ast

from ir import *
from parse import parse_shell_to_asts_interactive
from pash_graphviz import maybe_init_graphviz_dir
Expand Down
2 changes: 1 addition & 1 deletion compiler/preprocessor/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

import config
import ast_to_ast
from shell_ast import ast_to_ast
from ir import FileIdGen
from parse import parse_shell_to_asts, from_ast_objects_to_shell
from util import *
Expand Down
Empty file added compiler/shell_ast/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import config

from ast_util import *
from shell_ast.ast_util import *
from parse import from_ast_objects_to_shell_file


Expand Down
36 changes: 36 additions & 0 deletions compiler/ast_util.py → compiler/shell_ast/ast_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from definitions.ast_node import *
## TODO: These calls need to be moved here, they don't make sense in ir_utils
from ir_utils import string_to_argument, make_kv

## This class is used by the preprocessor in ast_to_ir
class PreprocessedAST:
Expand Down Expand Up @@ -51,3 +53,37 @@ def ast_match(ast_node, cases, *args):
return ast_match_untyped(ast_node, cases, *args)

return cases[ast_node.construct.value](*args)(ast_node)

##
## Make some nodes
##

def make_echo_ast(argument, var_file_path):
nodes = []
## Source variables if present
if(not var_file_path is None):
arguments = [string_to_argument("source"), string_to_argument(var_file_path)]

line_number = 0
node = make_kv('Command', [line_number, [], arguments, []])
nodes.append(node)

## Reset the exit status
variable_arg = make_kv('V', ['Normal', "false", 'pash_previous_exit_status', []])
arguments = [string_to_argument("exit"), [variable_arg]]
exit_node = make_kv('Command', [0, [], arguments, []])
node = make_kv('Subshell', [0, exit_node, []])
nodes.append(node)

## Reset the input arguments
variable_arg = make_kv('V', ['Normal', "false", 'pash_input_args', []])
arguments = [string_to_argument("set"), string_to_argument("--"), [variable_arg]]
set_node = make_kv('Command', [0, [], arguments, []])
nodes.append(set_node)

arguments = [string_to_argument("echo"), string_to_argument("-n"), argument]

line_number = 0
node = make_kv('Command', [line_number, [], arguments, []])
nodes.append(node)
return nodes

0 comments on commit 55966bb

Please sign in to comment.