annotate ppc/lib.c @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /* ppc/lib.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 #include "obx.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 #include <stdio.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 /* Primitives that can be called from picoPascal programs */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 unsigned prim_check = 0; /* Fake checksum for set of primitives */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 #define args (bp + HEAD + 1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 static void _new(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 (*(args[0].p)).x = malloc(args[1].i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 static void _argc(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 ob_res.i = saved_argc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 static void _argv(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 /* Buffer overflow waiting to happen */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 strcpy((char *) args[1].x, saved_argv[args[0].i]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 static void _print_num(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 printf("%d", args[0].i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 static void _print_string(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 printf("%s", args[0].x);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 static void _print_char(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 printf("%c", args[0].i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 static void _newline(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 printf("\n");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 static FILE *infile = NULL;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 static void _open_in(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 FILE *f = fopen((char *) args[0].x, "r");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 if (f == NULL) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 ob_res.i = 0; return;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 if (infile != NULL) fclose(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 infile = f;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 ob_res.i = 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 static void _close_in(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 if (infile == NULL) return;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 fclose(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 infile = NULL;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 static void _read_char(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 FILE *f = (infile == NULL ? stdin : infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 int ch = fgetc(f);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 *(args[0].x) = (ch == EOF ? 127 : ch);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 static void _pexit(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 value *bp = sp;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 exit(args[0].i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 void dltrap(value *sp) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 fprintf(stderr, "Oops: dltrap called!\n");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 exit(2);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 primitive *primtab[] = {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 interp, dltrap, _new, _open_in, _close_in, _read_char,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 _print_num, _print_string, _print_char, _newline,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 _argc, _argv, _pexit, NULL
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 };
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 char *primname[] = {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 "INTERP", "DLTRAP", "_new", "_open_in", "_close_in",
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 "_read_char", "_print_num", "_print_string",
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 "_print_char", "_newline", "_argc", "_argv", "_pexit"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 };
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93