-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscript.c
More file actions
91 lines (84 loc) · 2 KB
/
script.c
File metadata and controls
91 lines (84 loc) · 2 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* alt - abstract language tree // pancake<at>nopcode.org */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "alt.h"
int alt_word_is_num(const char *str) {
return (*str>='0' && *str<='9');
}
int alt_word_is_assign(const char *str) {
return (!strcmp (str, "=") || !strcmp (str, "+=") || !strcmp (str, "-="));
}
int alt_word_is_op(const char *str) {
if (str[1]=='\0')
switch(str[0]) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '|':
case '&':
case '%':
return 1;
}
return 0;
}
//-----------------//
int alt_script_run(AltState *st, AltNode *node) {
AltNode *onode = node;
if (node == NULL)
return 1;
if (!*node->str) {
// ignore
} else
if (*node->str=='$') {
// variable
if (node->down && node->down->down && alt_word_is_assign(node->down->str)) {
//printf("ASSIGN '%s' = '%s'\n", node->str, node->down->down->str);
setenv (node->str, node->down->down->str, 1);
onode = node->down->down;
}
} else {
if (!strcmp (node->str, "say")) {
node = alt_tree_child (node);
while (node) {
puts (node->str);
node = node->down;
}
} else
if (!strcmp (node->str, "system")) {
if (!node->right) {
onode = node->down;
node = node->down;
node = alt_tree_resolve (st, node->str);
if (node)
node = alt_tree_child (node);
} else node = alt_tree_child (node);
while (node) {
if (system (node->str) != 0) {
perror ("system");
break;
}
node = node->down;
}
} else
if (!strcmp (node->str, "exit")) {
node = alt_tree_child (node);
if (node) exit (atoi (node->str));
} else fprintf (stderr, "UNKNOWN (%s)\n", node->str);
}
return alt_script_run (st, onode->down);
}
int alt_script(AltState *st) {
AltNode *node;
AltTree *at = st->user;
if (at == NULL)
return st->cb_error (st, "No tree found.");
node = alt_tree_resolve (st, "main");
if (node == NULL)
return st->cb_error (st, "Cannot find 'main'.");
//alt_tree_walk(st);
node = alt_tree_child (node);
return alt_script_run (st, node);
}