2 * FML interpreter. Europagate, 1995
5 * Revision 1.3 1995/02/23 08:32:04 adam
8 * Revision 1.1 1995/02/09 14:33:36 adam
9 * Split source fml.c and define relevant build-in functions in separate
10 * files. New operators mult, div, not, llen implemented.
20 static struct fml_node *fml_exec_plus (Fml fml, struct fml_node *l,
23 int left_val, right_val;
25 fml_lr_values (fml, l, &left_val, r, &right_val);
26 return fml_mk_node_val (fml, left_val + right_val);
29 static struct fml_node *fml_exec_minus (Fml fml, struct fml_node *l,
32 int left_val, right_val;
34 fml_lr_values (fml, l, &left_val, r, &right_val);
35 return fml_mk_node_val (fml, left_val - right_val);
38 static struct fml_node *fml_exec_mult (Fml fml, struct fml_node *l,
41 int left_val, right_val;
43 fml_lr_values (fml, l, &left_val, r, &right_val);
44 return fml_mk_node_val (fml, left_val * right_val);
47 static struct fml_node *fml_exec_div (Fml fml, struct fml_node *l,
50 int left_val, right_val;
52 fml_lr_values (fml, l, &left_val, r, &right_val);
53 return fml_mk_node_val (fml, left_val / right_val);
56 static struct fml_node *fml_exec_incr (Fml fml, struct fml_node **lp,
59 struct fml_node *fn = NULL;
60 struct fml_sym_info *info;
64 info = fml_sym_lookup (fml->sym_tab, tp->tokenbuf);
66 if (info->kind == FML_VAR && info->body && info->body->is_atom)
71 val = fml_atom_val (info->body->p[0]);
72 fml_node_delete (fml, info->body);
73 sprintf (arg, "%d", val+1);
74 info->body = fn = fml_node_alloc (fml);
76 fn->p[0] = fml_atom_alloc (fml, arg);
83 static struct fml_node *fml_exec_decr (Fml fml, struct fml_node **lp,
86 struct fml_node *fn = NULL;
87 struct fml_sym_info *info;
91 info = fml_sym_lookup (fml->sym_tab, tp->tokenbuf);
93 if (info->kind == FML_VAR && info->body && info->body->is_atom)
98 val = fml_atom_val (info->body->p[0]);
99 sprintf (arg, "%d", val-1);
100 info->body = fn = fml_node_alloc (fml);
102 fn->p[0] = fml_atom_alloc (fml, arg);
105 fml_cmd_lex (lp, tp);
109 void fml_arit_init (Fml fml)
111 struct fml_sym_info *sym_info;
113 sym_info = fml_sym_add (fml->sym_tab, "plus");
114 sym_info->kind = FML_CBINARY;
115 sym_info->binary = fml_exec_plus;
116 sym_info = fml_sym_add (fml->sym_tab, "minus");
117 sym_info->kind = FML_CBINARY;
118 sym_info->binary = fml_exec_minus;
120 sym_info = fml_sym_add (fml->sym_tab, "mult");
121 sym_info->kind = FML_CBINARY;
122 sym_info->binary = fml_exec_mult;
123 sym_info = fml_sym_add (fml->sym_tab, "div");
124 sym_info->kind = FML_CBINARY;
125 sym_info->binary = fml_exec_div;
127 sym_info = fml_sym_add (fml->sym_tab, "incr");
128 sym_info->kind = FML_CPREFIX;
129 sym_info->prefix = fml_exec_incr;
130 sym_info = fml_sym_add (fml->sym_tab, "decr");
131 sym_info->kind = FML_CPREFIX;
132 sym_info->prefix = fml_exec_decr;