diff options
| author | mryouse | 2022-05-20 02:25:44 +0000 | 
|---|---|---|
| committer | mryouse | 2022-05-20 02:25:44 +0000 | 
| commit | 5381b2307a269dfe26085e494a6b4c1a7277112e (patch) | |
| tree | e4da06cf7d0444388f3b87c57224c69be88fcf3c /std.py | |
| parent | 0b8f41f417e61f81c15c4495dfbc793dea4ca7a8 (diff) | |
cleanup old files
Diffstat (limited to 'std.py')
| -rw-r--r-- | std.py | 321 | 
1 files changed, 0 insertions, 321 deletions
| @@ -1,321 +0,0 @@ -from tokens import * -import sys -from collections import namedtuple -import subprocess - -FuncImpl = namedtuple("FuncImpl", ("func", "impl")) - -STD = {} -DEBUG = True -USER = {} - - -def _get_debug(): -    return DEBUG - -def std_exit(status=None): -    out = 0 if status is None else status.value -    sys.exit(out) -    return NebNil() - -def std_print(arg): -    print(arg.value) -    return NebNil() - -def std_print_all(arg): -    items = arg.items -    for idx, item in enumerate(items): -        if isinstance(item, NebExpression): -            items[idx] = evaluate_expression(item) -        elif not isinstance(item, NebString): -            raise exception("print-all needs all strings!") -    for x in items: -        std_print(x) -    return NebNil() -     -def std_debug_on(): -    global DEBUG -    DEBUG = True -    return NebNil() - -def std_debug_off(): -    global DEBUG -    DEBUG = False -    return NebNil() - -# math -def std_add(arg, rest): -    float_type = True if isinstance(arg, NebFloat) else False -    if isinstance(rest, NebNumber): -        acc = arg.value + rest.value -        float_type = float_type or isinstance(rest, NebFloat) -    else: -        acc = arg.value -        for item in rest: -            acc += item.value -            float_type = float_type or isinstance(item, NebFloat) -    if float_type: -        return NebFloat(acc) -    else: -        return NebInt(acc) - -def std_subtract(arg, rest): -    float_type = True if isinstance(arg, NebFloat) else False -    if isinstance(rest, NebNumber): -        acc = arg.value - rest.value -        float_type = float_type or isinstance(rest, NebFloat) -    else: -        acc = arg.value -        for item in rest: -            acc -= item.value -            float_type = float_type or isinstance(item, NebFloat) -    if float_type: -        return NebFloat(acc) -    else: -        return NebInt(acc) - -def std_multiply(arg, rest): -    float_type = True if isinstance(arg, NebFloat) else False -    if isinstance(rest, NebNumber): -        acc = arg.value * rest.value -        float_type = float_type or isinstance(rest, NebFloat) -    else: -        acc = arg.value -        for item in rest: -            acc *= item.value -            float_type = float_type or isinstance(item, NebFloat) -    if float_type: -        return NebFloat(acc) -    else: -        return NebInt(acc) - -# strings -def std_concat(arg, rest): -    out = f"{arg.value}" -    if isinstance(rest, NebString): -        return NebString(f"{out}{rest.value}") -    else: -        return NebString(f"{out}" + "".join(f"{item.value}" for item in rest)) - -# flow control -def std_if(cond, t_branch, f_branch=None): -    if cond.value: -        if isinstance(t_branch, NebExpression): -            return evaluate_expression(t_branch) -        else: -            return t_branch -    elif f_branch is not None: -        if isinstance(f_branch, NebExpression): -            return evaluate_expression(f_branch) -        else: -            return f_branch -    #return NebBool(True) -    return NebNil() - -# type validation -def std_is_string(arg): -    if isinstance(arg, NebString): -        return NebBool(True) -    else: -        return NebBool(False) - -def std_is_int(arg): -    if isinstance(arg, NebInt): -        return NebBool(True) -    else: -        return NebBool(False) - -def std_is_float(arg): -    if isinstance(arg, NebFloat): -        return NebBool(True) -    else: -        return NebBool(False) - -def std_is_number(arg): -    if isinstance(arg, NebNumber): -        return NebBool(True) -    else: -        return NebBool(False) - -def std_is_bool(arg): -    if isinstance(arg, NebBool): -        return NebBool(True) -    else: -        return NebBool(False) - -# type conversion -def std_literal_to_string(arg): -    if isinstance(arg, NebString): -        return arg -    else: -        return NebString(f"{arg.value}".lower()) - -# shell -def std_shell(arg): -    assert isinstance(arg, NebString) -    lst = arg.value.split(" ") -    ret = subprocess.run(lst, capture_output=True, check=True) -    stdout_as_list = ret.stdout.decode("utf-8").split("\n")[:-1] -    return NebList(list(NebString(x) for x in stdout_as_list)) - -def std_shell_pipe(args): -    prev = None -    stdout = subprocess.PIPE -    for idx, arg in enumerate(args.items): -        if idx == len(args.items) - 1: -            stdout = None -        lst = arg.value.split(" ") -        if prev is None: -            if stdout is None: -                ret = subprocess.run(lst, capture_output=True, check=True) -            else: -                ret = subprocess.run(lst, stdout=stdout, check=True) -        else: -            if stdout is None: -                ret = subprocess.run(lst, input=prev.stdout, capture_output=True, check=True) -            else: -                ret = subprocess.run(lst, stdout=stdout, input=prev.stdout, check=True) - -        prev = ret - -    stdout_as_list = prev.stdout.decode("utf-8").split("\n")[:-1] -    return NebList(list(NebString(x) for x in stdout_as_list)) - -def evaluate_expression(expr): -    if not expr.symbol.name in STD: -        raise Exception(f"no such symbol: {expr.symbol.name}") -    this_func = STD[expr.symbol.name] - -    # try to match the signature -    validated = 0 -    in_sig = expr.args -    for value in this_func: -        validated = 0 -        sig = value.func.args - -        # if many is defined, it can take one or more of them -        many = value.func.many - -        # if we need exact arguments, the signatures must match length -        if many is None and len(sig) != len(in_sig): -            continue -        # if we have "many", must have at least the exact length -        elif len(in_sig) < len(sig): -            continue - -        # loop through explicits -        for idx in range(len(sig)): -            if isinstance(in_sig[idx], sig[idx]): -                validated += 1 - -        # return if we've found it -        if validated == len(in_sig): -            ret = value.impl(*(expr.args)) -            return ret - -        # loop through the remainder of in_sig -        many_idx = 0 -        first_args = in_sig[0:len(sig)] -        rest_args = in_sig[len(sig):] -        for arg in rest_args: -            if isinstance(arg, many[many_idx]): -                validated += 1 -            if len(many) - 1 == many_idx: -                many_idx = 0 -            else: -                many_idx += 1 - -        if validated == len(in_sig): -            # move end of first list to beginning of second list -            new_args = first_args[:-1] -            new_rest = [first_args[-1]] + rest_args -            new_args.append(new_rest) -            ret = value.impl(*(new_args)) -            return ret - -    # evaluate inner expressions, if possible/necessary -    for idx, arg in enumerate(expr.args): -        if isinstance(arg, NebExpression): -            expr.args[idx] = evaluate_expression(arg) -            return evaluate_expression(expr) - -    raise Exception(f"'{expr.symbol.name}' called with unknown signature: '{expr.in_sig()}'") - -def build_std(): -    print_string = FuncImpl(NebFunction("print", [NebString], NebNil), std_print) -    STD["print"] = [print_string] - -    print_all = FuncImpl(NebFunction("print-all", [NebList], NebNil), std_print_all) -    STD["print-all"] = [print_all] - -    exit_ = FuncImpl(NebFunction("exit", [], NebNil), std_exit) -    exit_int = FuncImpl(NebFunction("exit", [NebInt], NebNil), std_exit) -    STD["exit"] = [exit_, exit_int] - -    debug_on = FuncImpl(NebFunction("debug-on", [], NebNil), std_debug_on) -    STD["debug-on"] = [debug_on] -    debug_off = FuncImpl(NebFunction("debug-off", [], NebNil), std_debug_off) -    STD["debug-off"] = [debug_off] - -    # arithmetic -    add = FuncImpl(NebFunction("+", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_add) -    STD["+"] = [add] - -    subtract = FuncImpl(NebFunction("-", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_subtract) -    STD["-"] = [subtract] - -    multiply = FuncImpl(NebFunction("*", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_multiply) -    STD["*"] = [multiply] - -    # strings -    concat_string_string = FuncImpl(NebFunction("concat", [NebString, NebString], NebString, [NebString]), std_concat) -    STD["concat"] = [concat_string_string] - -    # flow control -    #if_bool_any_any = FuncImpl(NebFunction("if", [NebBool, NebAny, NebAny], NebAny), std_if) -    #if_bool_any = FuncImpl(NebFunction("if", [NebBool, NebAny], NebAny), std_if) -    if_bool_expr_expr = FuncImpl(NebFunction("if", [NebBool, NebExpression, NebExpression], NebAny), std_if) -    if_bool_expr_any = FuncImpl(NebFunction("if", [NebBool, NebExpression, NebAny], NebAny), std_if) -    if_bool_any_expr = FuncImpl(NebFunction("if", [NebBool, NebAny, NebExpression], NebAny), std_if) -    if_bool_any_any = FuncImpl(NebFunction("if", [NebBool, NebAny, NebAny], NebAny), std_if) -    if_bool_expr = FuncImpl(NebFunction("if", [NebBool, NebExpression], NebAny), std_if) -    if_bool_any = FuncImpl(NebFunction("if", [NebBool, NebAny], NebAny), std_if) -    STD["if"] = [ -        if_bool_expr_expr, -        if_bool_expr_any, -        if_bool_any_expr, -        if_bool_any_any, -        if_bool_expr, -        if_bool_any] - -    # type checking -    is_string = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_string) -    STD["string?"] = [is_string] -    is_int = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_int) -    STD["int?"] = [is_int] -    is_float = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_float) -    STD["float?"] = [is_float] -    is_number = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_number) -    STD["number?"] = [is_number] -    is_bool = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_bool) -    STD["bool?"] = [is_bool] - -    # type conversion -    int_to_string = FuncImpl(NebFunction("int->string", [NebInt], NebString), std_literal_to_string) -    STD["int->string"] = [int_to_string] -    float_to_string = FuncImpl(NebFunction("float->string", [NebFloat], NebString), std_literal_to_string) -    STD["float->string"] = [float_to_string] -    number_to_string = FuncImpl(NebFunction("number->string", [NebNumber], NebString), std_literal_to_string) -    STD["number->string"] = [number_to_string] -    bool_to_string = FuncImpl(NebFunction("bool->string", [NebBool], NebString), std_literal_to_string) -    STD["bool->string"] = [bool_to_string] -    to_string = FuncImpl(NebFunction("->string", [NebLiteral], NebString), std_literal_to_string) -    STD["->string"] = [to_string] - -    # shell -    shell_string = FuncImpl(NebFunction("$", [NebString], NebList), std_shell) -    STD["$"] = [shell_string] -    shell_pipe = FuncImpl(NebFunction("$|", [NebList], NebList), std_shell_pipe) -    STD["$|"] = [shell_pipe] - -build_std() | 
