-
Notifications
You must be signed in to change notification settings - Fork 1
/
foreign_interface.c
64 lines (55 loc) · 1.55 KB
/
foreign_interface.c
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
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include "environment.h"
#include "display.h"
#include "interpreter.h"
static int is_num(Object obj){
return obj.literal.type == LIT_INT || obj.literal.type == LIT_DOUBLE;
}
static double get_val(Literal lit){
return lit.type == LIT_INT ? lit.iVal : lit.dVal;
}
double get_double(char *identifer, int line, Environment *env){
Object o = env_get(identifer, line, env);
if(o.type != OBJECT_LITERAL || !is_num(o)){
printf(runtime_error("Expected numeric value!"), line);
stop();
}
return get_val(o.literal);
}
long get_long(char *identifer, int line, Environment *env){
Object o = env_get(identifer, line, env);
if(o.type != OBJECT_LITERAL || o.literal.type != LIT_INT){
printf(runtime_error("Expected integer value!"), line);
stop();
}
return o.literal.iVal;
}
char* get_string(char *identifer, int line, Environment *env){
Object o = env_get(identifer, line, env);
if(o.type != OBJECT_LITERAL || o.literal.type != LIT_STRING){
printf(runtime_error("Expected string value!"), line);
stop();
}
return o.literal.sVal;
}
Object fromDouble(double d){
Object o;
o.type = OBJECT_LITERAL;
o.literal.type = LIT_DOUBLE;
o.literal.dVal = d;
return o;
}
Object fromLong(long l){
Object o;
o.type = OBJECT_LITERAL;
o.literal.type = LIT_INT;
o.literal.dVal = l;
return o;
}
Object fromString(char *string){
Object o;
o.type = OBJECT_LITERAL;
o.literal.type = LIT_STRING;
o.literal.sVal = string;
return o;
}