Exemple #1
0
def _values():
    return {
        # write: str -> str -> cmd[()]
        "write": ("str", "str", n_cmd_type.with_typevars(["unit"])),
        # append: str -> str -> cmd[()]
        "append": ("str", "str", n_cmd_type.with_typevars(["unit"])),
        # read: str -> cmd[maybe[str]]
        "read": (
            "str",
            n_cmd_type.with_typevars([n_maybe_type.with_typevars(["str"])]),
        ),
        "writeBytes": (
            "str",
            n_list_type.with_typevars(["int"]),
            n_cmd_type.with_typevars(["unit"]),
        ),
        "appendBytes": (
            "str",
            n_list_type.with_typevars(["int"]),
            n_cmd_type.with_typevars(["unit"]),
        ),
        "readBytes": (
            "str",
            n_cmd_type.with_typevars(
                [n_maybe_type.with_typevars([n_list_type.with_typevars(["int"])])]
            ),
        ),
        "getFiles": ("str", n_cmd_type.with_typevars([n_list_type.with_typevars([["bool", "str"]])])),
    }
Exemple #2
0
def _values():
    return {
        "request": (
            "str",
            "str",
            n_maybe_type.with_typevars([json_value_type]),
            n_maybe_type.with_typevars([json_value_type]),
            n_cmd_type.with_typevars(
                [{"code": "int", "response": "str", "return": json_value_type}]
            ),
        ),
        "sendMultipartForm": (
            "str",
            "str",
            n_maybe_type.with_typevars([json_value_type]),
            n_list_type.with_typevars([{
                "name": "str",
                "data": ["bool", n_list_type.with_typevars(["int"])],
                "filename": n_maybe_type.with_typevars(["str"]),
                "contentType": n_maybe_type.with_typevars(["str"]),
            }]),
            n_cmd_type.with_typevars(
                [{"code": "int", "response": "str", "return": json_value_type}]
            ),
        ),
        "createServer": (
            "int",
            (
                "str",
                "str",
                json_value_type,
                n_cmd_type.with_typevars(
                    [
                        {
                            "responseCode": "int",
                            "data": n_list_type.with_typevars(["int"]),
                            "headers": n_map_type.with_typevars(["str", "str"]),
                            "mimetype": "str",
                        }
                    ]
                ),
            ),  # [path:str requestType:str data:json.value] -> cmd[{ responseCode:int; data:list[int]; headers:map[str, str]; mimetype:str }]
            n_cmd_type.with_typevars(["unit"]),
        ),
    }
Exemple #3
0
def _values():
    return {
        # connect: connectOptions -> str -> cmd[maybe[str]]
        "connect": (
            connect_options_type,
            "str",
            n_cmd_type.with_typevars([n_maybe_type.with_typevars(["str"])]),
        ),
        # createServer: setupOptions -> int -> cmd[()]
        "createServer": (
            setup_options_type,
            "int",
            n_cmd_type.with_typevars(["unit"]),
        ),
    }
Exemple #4
0
def _values():
    return {
        # json.value enum values
        "null":
        json_value_type,
        "string": ("str", json_value_type),
        "number": ("float", json_value_type),
        "boolean": ("bool", json_value_type),
        "array":
        (n_list_type.with_typevars([json_value_type]), json_value_type),
        "object":
        (n_map_type.with_typevars(["str", json_value_type]), json_value_type),
        # JSON parsing/stringifying
        "parse": ("str", json_value_type),
        "parseSafe": ("str", n_maybe_type.with_typevars([json_value_type])),
        "stringify": (json_value_type, "str"),
    }
Exemple #5
0
def add_funcs(global_scope):
    global_scope.variables["none"] = Variable(n_maybe_type, none)

    global_scope.add_native_function(
        "range",
        [("start", "int"), ("end", "int"), ("step", "int")],
        n_list_type.with_typevars(["int"]),
        range_without_error,
    )

    print_generic = NGenericType("t")
    global_scope.add_native_function("print", [("val", print_generic)],
                                     print_generic, special_print)

    print_with_end_generic = NGenericType("t")
    global_scope.add_native_function(
        "printWithEnd",
        [("end", "str"), ("val", print_with_end_generic)],
        print_with_end_generic,
        special_print_with_end,
    )

    global_scope.add_native_function(
        "yes",
        [("value", maybe_generic)],
        n_maybe_type.with_typevars([maybe_generic]),
        yes,
    )

    global_scope.add_native_function(
        "ok",
        [("value", result_ok_generic)],
        n_result_type.with_typevars([result_ok_generic, result_err_generic]),
        ok,
    )
    global_scope.add_native_function(
        "err",
        [("error", result_err_generic)],
        n_result_type.with_typevars([result_ok_generic, result_err_generic]),
        err,
    )
    then_generic_in = NGenericType("a")
    then_generic_out = NGenericType("b")
    global_scope.add_native_function(
        "then",
        [
            (
                "thenFunction",
                (then_generic_in, n_cmd_type.with_typevars([then_generic_out
                                                            ])),
            ),
            ("cmd", n_cmd_type.with_typevars([then_generic_in])),
        ],
        n_cmd_type.with_typevars([then_generic_out]),
        cmd_then,
    )
    parallel_generic = NGenericType("a")
    global_scope.add_native_function(
        "parallel",
        [
            ("cmd", n_cmd_type.with_typevars([parallel_generic])),
        ],
        n_cmd_type.with_typevars(
            [n_cmd_type.with_typevars([parallel_generic])]),
        cmd_parallel,
    )
    map_from_generic_key = NGenericType("k")
    map_from_generic_value = NGenericType("v")
    global_scope.add_native_function(
        "mapFrom",
        [(
            "entries",
            n_list_type.with_typevars(
                [[map_from_generic_key, map_from_generic_value]]),
        )],
        n_map_type.with_typevars(
            [map_from_generic_key, map_from_generic_value]),
        map_from,
    )
    map_get_generic_key = NGenericType("k")
    map_get_generic_value = NGenericType("v")
    global_scope.add_native_function(
        "getValue",
        [
            ("key", map_get_generic_key),
            (
                "map",
                n_map_type.with_typevars(
                    [map_get_generic_key, map_get_generic_value]),
            ),
        ],
        n_maybe_type.with_typevars([map_get_generic_value]),
        map_get,
    )
    entries_generic_key = NGenericType("k")
    entries_generic_value = NGenericType("v")
    global_scope.add_native_function(
        "entries",
        [(
            "map",
            n_map_type.with_typevars(
                [entries_generic_key, entries_generic_value]),
        )],
        n_list_type.with_typevars(
            [[entries_generic_key, entries_generic_value]]),
        entries,
    )
    into_module_generic_value = NGenericType("m")
    global_scope.add_native_function(
        "intoModule",
        [("possibleModule", into_module_generic_value)],
        n_maybe_type.with_typevars([n_module_type.with_typevars([])]),
        to_module,
    )

    global_scope.add_native_function(
        "exit",
        [("exitCode", "int")],
        "unit",
        sys.exit,
    )

    global_scope.types["str"] = "str"
    global_scope.types["char"] = "char"
    global_scope.types["int"] = "int"
    global_scope.types["float"] = "float"
    global_scope.types["bool"] = "bool"
    global_scope.types["list"] = n_list_type
    global_scope.types["map"] = n_map_type
    global_scope.types["cmd"] = n_cmd_type
    global_scope.types["maybe"] = n_maybe_type
    global_scope.types["result"] = n_result_type
    global_scope.types["module"] = n_module_type

    global_scope.add_internal_trait(
        "str",
        "len",
        [
            ("self", "str"),
        ],
        "int",
        len,
    )

    len_trait_generic = NGenericType("t")
    global_scope.add_internal_trait(
        "list",
        "len",
        [
            ("self", "str"),
        ],
        "int",
        len,
    )

    default_trait_generic = NGenericType("t")
    global_scope.add_internal_trait(
        "maybe",
        "default",
        [
            ("self", n_maybe_type.with_typevars([default_trait_generic])),
            ("default", default_trait_generic),
        ],
        default_trait_generic,
        with_default,
    )

    global_scope.add_internal_trait(
        "int",
        "toString",
        [("self", "int")],
        "str",
        lambda v: str(v),
    )

    global_scope.add_internal_trait(
        "float",
        "round",
        [("self", "float")],
        "int",
        round_without_error,
    )

    global_scope.add_internal_trait(
        "float",
        "floor",
        [("self", "float")],
        "int",
        floor_without_error,
    )

    global_scope.add_internal_trait(
        "float",
        "ceil",
        [("self", "float")],
        "int",
        ceil_without_error,
    )

    global_scope.add_internal_trait(
        "char",
        "charCode",
        [("self", "char")],
        "int",
        ord,
    )

    global_scope.add_internal_trait(
        "int",
        "intCode",
        [("self", "int")],
        "char",
        char_with_replace,
    )

    global_scope.add_internal_trait(
        "str",
        "charAt",
        [("self", "str"), ("location", "int")],
        n_maybe_type.with_typevars(["char"]),
        char_at,
    )

    global_scope.add_internal_trait(
        "str",
        "substring",
        [("self", "str"), ("start", "int"), ("end", "int")],
        "str",
        substr,
    )

    global_scope.add_internal_trait(
        "int",
        "toFloat",
        [("self", "int")],
        "float",
        lambda v: float(v),
    )

    global_scope.add_internal_trait(
        "str",
        "split",
        [("self", "str"), ("splitter", "char")],
        n_list_type.with_typevars(["str"]),
        lambda string, splitter: string.split(splitter),
    )

    global_scope.add_internal_trait("str", "strip", [("self", "str")], "str",
                                    trim)

    global_scope.add_internal_trait("str", "parseFloat", [("self", "str")],
                                    n_maybe_type.with_typevars(["float"]),
                                    parse_float)

    global_scope.add_internal_trait("str", "parseInt", [("self", "str")],
                                    n_maybe_type.with_typevars(["int"]),
                                    parse_int)

    item_at_trait_generic = NGenericType("t")
    global_scope.add_internal_trait(
        "list",
        "itemAt",
        [("self", n_list_type.with_typevars([item_at_trait_generic])),
         ("index", "int")],
        n_maybe_type.with_typevars([item_at_trait_generic]),
        item_at,
    )

    append_trait_generic = NGenericType("t")
    global_scope.add_internal_trait(
        "list",
        "append",
        [
            ("self", n_list_type.with_typevars([append_trait_generic])),
            ("item", append_trait_generic),
        ],
        n_list_type.with_typevars([append_trait_generic]),
        lambda l, i: l + [i],
    )

    subsection_trait_generic = NGenericType("t")
    global_scope.add_internal_trait(
        "list",
        "subsection",
        [
            ("list", n_list_type.with_typevars([subsection_trait_generic])),
            ("lower", "int"),
            ("upper", "int"),
        ],
        n_list_type.with_typevars([subsection_trait_generic]),
        subsection_list,
    )

    filter_map_trait_generic_a = NGenericType("a")
    filter_map_trait_generic_b = NGenericType("b")
    global_scope.add_internal_trait(
        "list",
        "filterMap",
        [("list", n_list_type.with_typevars([filter_map_trait_generic_a])),
         (
             "function",
             (
                 filter_map_trait_generic_a,
                 n_maybe_type.with_typevars([filter_map_trait_generic_b]),
             ),
         )],
        n_list_type.with_typevars([filter_map_trait_generic_b]),
        filter_map,
    )

    global_scope.add_internal_trait(
        "module",
        "getUnitTestResults",
        [("possibleModule", n_module_type)],
        n_list_type.with_typevars([{
            "hasPassed":
            "bool",
            "fileLine":
            "int",
            "unitTestType":
            "str",
            "possibleTypes":
            n_maybe_type.with_typevars([["str", "str"]]),
        }]),
        lambda module: scope.unit_test_results[module.mod.mod_name][:],
    )
Exemple #6
0
in_map_generic = NGenericType("k")
in_value_generic = NGenericType("v")

access_list_generic = NGenericType("t")
access_map_generic = NGenericType("k")
access_value_generic = NGenericType("v")

or_maybe_generic = NGenericType("t")

not_maybe_generic = NGenericType("t")

binary_operation_types = {
    "OR": [
        ("bool", "bool", "bool"),
        ("int", "int", "int"),
        (n_maybe_type.with_typevars([or_maybe_generic]), or_maybe_generic, or_maybe_generic)
    ],
    "AND": [("bool", "bool", "bool"), ("int", "int", "int")],
    "XOR": [("bool", "bool", "bool"), ("int", "int", "int")],
    "ADD": [
        ("int", "int", "int"),
        ("float", "float", "float"),
        ("str", "str", "str"),
        ("char", "char", "str"),
        ("str", "char", "str"),
        ("char", "str", "str"),
        (n_list_type, n_list_type, n_list_type),
    ],
    "SUBTRACT": [("int", "int", "int"), ("float", "float", "float")],
    "MULTIPLY": [("int", "int", "int"), ("float", "float", "float")],
    "DIVIDE": [("int", "int", "int"), ("float", "float", "float")],
Exemple #7
0
    "ip": ["int", "int", "int", "int"],
    "uuid": "str",
}

# alias setupOptions = {
#   onConnect: (user, str) -> cmd[bool]
#   onMessage: (user, str) -> cmd[bool]
#   onDisconnect: (user, { code: int; reason: str }) -> cmd[bool]
# }
setup_options_type = {
    "onConnect": (user_type, "str", n_cmd_type.with_typevars(["bool"])),
    "onMessage": (user_type, "str", n_cmd_type.with_typevars(["bool"])),
    "onDisconnect": (
        user_type,
        n_maybe_type.with_typevars([{
            "code": "int",
            "reason": "str",
        }]),
        n_cmd_type.with_typevars(["bool"]),
    ),
}


async def connect(options, url):
    debug = os.environ.get("N_WS_DEBUG") == "experimental"
    if debug:
        print(
            f"{Fore.YELLOW}Websocket debugging is experimental and may be removed in the future.{Style.RESET_ALL}"
        )
        print(f"[{url}] {Fore.BLUE}Connecting.{Style.RESET_ALL}")

        async def manual_send():