This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbong_builtins.py
53 lines (48 loc) · 2.33 KB
/
bong_builtins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import bongtypes
from bongvalues import ValueList
# TODO Currently, the argument checker function raise BongtypeExceptions
# which are converted to TypecheckerExceptions in typechecker.py. This
# approach does not allow to specify the error location more specifically
# here because only TypecheckerExpcetion takes an ast.Node as a second
# argument which is used to calculate an error location.
#
# The same problem holds true for BongtypeExceptions generated in
# bongtypes.py.
def builtin_func_len(args):
return ValueList([len(args[0])])
def check_len(argument_types : bongtypes.TypeList) -> bongtypes.TypeList:
if len(argument_types)!=1:
raise bongtypes.BongtypeException("Function 'len' expects one single argument.")
arg = argument_types[0]
if not isinstance(arg, bongtypes.Array) and not isinstance(arg, bongtypes.String):
raise bongtypes.BongtypeException("Function 'len' expects an Array or a String, '{}' was found instead.".format(arg))
return bongtypes.TypeList([bongtypes.Integer()])
def builtin_func_get_argv(args):
import sys
return ValueList([sys.argv])
def check_get_argv(argument_types : bongtypes.TypeList) -> bongtypes.TypeList:
if len(argument_types)!=0:
raise bongtypes.BongtypeException("Function 'get_argv' expects no arguments.")
return bongtypes.TypeList([bongtypes.Array(bongtypes.String())])
def builtin_func_append(args):
array = args[0]
element = args[1]
array.append(element)
return ValueList([array])
def check_append(argument_types: bongtypes.TypeList) -> bongtypes.TypeList:
if len(argument_types)!=2:
raise bongtypes.BongtypeException("Function 'append' expects exactly two arguments.")
if not isinstance(argument_types[0], bongtypes.Array):
raise bongtypes.BongtypeException("Function 'append' expects first parameter of type Array.")
if not argument_types[0].contained_type.sametype(argument_types[1]):
raise bongtypes.BongtypeException("Appended type does not match array type in function 'append'.")
return bongtypes.TypeList([argument_types[0]])
functions = {
#"call": self.callprogram,
"len": (
builtin_func_len, # function to call
check_len # function to check params
),
"get_argv": (builtin_func_get_argv, check_get_argv),
"append": (builtin_func_append, check_append),
}