Basic: implement CALL in basicpp.py and use it.
[jackhill/mal.git] / basic / env.in.bas
1
2 REM ENV_NEW(O) -> R
3 ENV_NEW:
4 REM allocate the data hashmap
5 GOSUB HASHMAP
6 ET=R
7
8 REM set the outer and data pointer
9 T=13:L=R:N=O:GOSUB ALLOC
10 AY=ET:GOSUB RELEASE: REM environment takes ownership
11 RETURN
12
13 REM see RELEASE types.in.bas for environment cleanup
14
15 REM ENV_NEW_BINDS(O, BI, EX) -> R
16 ENV_NEW_BINDS:
17 GOSUB ENV_NEW
18 E=R
19 REM process bindings
20 ENV_NEW_BINDS_LOOP:
21 IF Z%(BI,1)=0 THEN R=E:RETURN
22 REM get/deref the key from BI
23 R=BI+1:GOSUB DEREF_R
24 K=R
25
26 IF S$(Z%(K,1))="&" THEN GOTO EVAL_NEW_BINDS_VARGS
27
28 EVAL_NEW_BINDS_1x1:
29 REM get/deref the key from EX
30 R=EX+1:GOSUB DEREF_R
31 V=R
32 REM set the binding in the environment data
33 GOSUB ENV_SET
34 REM go to next element of BI and EX
35 BI=Z%(BI,1)
36 EX=Z%(EX,1)
37 GOTO ENV_NEW_BINDS_LOOP
38
39 EVAL_NEW_BINDS_VARGS:
40 REM get/deref the key from next element of BI
41 BI=Z%(BI,1)
42 R=BI+1:GOSUB DEREF_R
43 K=R
44 REM the value is the remaining list in EX
45 A=EX:T=6:GOSUB FORCE_SEQ_TYPE
46 V=R
47 REM set the binding in the environment data
48 GOSUB ENV_SET
49 R=E
50 AY=V:GOSUB RELEASE: REM list is owned by environment
51 RETURN
52
53 REM ENV_SET(E, K, V) -> R
54 ENV_SET:
55 H=Z%(E,1)
56 GOSUB ASSOC1
57 Z%(E,1)=R
58 R=V
59 RETURN
60
61 REM ENV_SET_S(E, K$, V) -> R
62 ENV_SET_S:
63 H=Z%(E,1)
64 GOSUB ASSOC1_S
65 Z%(E,1)=R
66 R=V
67 RETURN
68
69 REM ENV_FIND(E, K) -> R
70 REM Returns environment (R) containing K. If found, value found is
71 REM in T4
72 SUB ENV_FIND
73 EF=E
74 ENV_FIND_LOOP:
75 H=Z%(EF,1)
76 REM More efficient to use GET for value (R) and contains? (T3)
77 GOSUB HASHMAP_GET
78 REM if we found it, save value in T4 for ENV_GET
79 IF T3=1 THEN T4=R:GOTO ENV_FIND_DONE
80 EF=Z%(EF+1,1): REM get outer environment
81 IF EF<>-1 THEN GOTO ENV_FIND_LOOP
82 ENV_FIND_DONE:
83 R=EF
84 END SUB
85
86 REM ENV_GET(E, K) -> R
87 ENV_GET:
88 CALL ENV_FIND
89 IF R=-1 THEN R=0:ER=-1:ER$="'"+S$(Z%(K,1))+"' not found":GOTO ENV_GET_RETURN
90 R=T4:GOSUB DEREF_R
91 Z%(R,0)=Z%(R,0)+32
92 GOTO ENV_GET_RETURN