aboutsummaryrefslogtreecommitdiffstats
path: root/shell.c
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2018-02-20 22:21:46 -0500
committerClyne Sullivan <tullivan99@gmail.com>2018-02-20 22:21:46 -0500
commitf414175e0cf08e02d65ca09028641ac2adbd0f8e (patch)
tree76637b32274f64975b4d225aea6573681b5dc9fe /shell.c
parent5d85baa270b046eedf472d4cb851fe17fb608fc5 (diff)
stdlib independence, fixes for calculator
Diffstat (limited to 'shell.c')
-rw-r--r--shell.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/shell.c b/shell.c
index c63b1bc..682213c 100644
--- a/shell.c
+++ b/shell.c
@@ -1,4 +1,5 @@
#include <parser.h>
+#include <builtins.h>
#include "stack.h"
@@ -9,7 +10,7 @@
int s_put(interpreter *it)
{
char *s = igetarg_string(it, 0);
- printf("%s\n", s);
+ printf("%s", s);
return 0;
}
@@ -36,6 +37,29 @@ int s_type(interpreter *it)
return 0;
}
+int input(interpreter *it)
+{
+ variable *v = igetarg(it, 0);
+ v->valtype = STRING;
+ v->svalue = malloc(128);
+ unsigned int unused;
+ getline(&v->svalue, &unused, stdin);
+ *strchr(v->svalue, '\n') = '\0';
+ return 0;
+}
+
+int concat(interpreter *it)
+{
+ variable *v = igetarg(it, 0);
+ char *s = igetarg_string(it, 1);
+ char *new = malloc(strlen(v->svalue) + strlen(s) + 1);
+ strcpy(new, v->svalue);
+ strcpy(new + strlen(v->svalue), s);
+ new[strlen(v->svalue) + strlen(s)] = 0;
+ v->svalue = new;
+ return 0;
+}
+
int quit(interpreter *it)
{
(void)it;
@@ -43,6 +67,21 @@ int quit(interpreter *it)
return 0;
}
+int expr(interpreter *it)
+{
+ variable *v = igetarg(it, 0);
+ variable *r = igetarg(it, 1);
+ int len = strlen(v->svalue);
+ char *s = malloc(len + 1);
+ strcpy(s, v->svalue);
+ s[len] = 0;
+ variable *q = idoexpr(it, s);
+ r->valtype = q->valtype;
+ r->value = q->value;
+ r->svalue = q->svalue;
+ return 0;
+}
+
int main(int argc, char **argv)
{
interpreter interp;
@@ -62,6 +101,9 @@ int main(int argc, char **argv)
inew_cfunc(&interp, "print", s_put);
inew_cfunc(&interp, "tp", s_type);
inew_cfunc(&interp, "q", quit);
+ inew_cfunc(&interp, "gets", input);
+ inew_cfunc(&interp, "concat", concat);
+ inew_cfunc(&interp, "expr", expr);
char *line = 0;