Hi guys,
so for this assignment I have to implement a BASIC like interpreter. I have 4 files, and I know how to handle 2 of them with some ideas on the other 2, but I'm having trouble with my very first file implement.
input.c reads in a Stmt* from main and returns a Stmt pointer for context, I supply the main.c and interpret.h files
Code:
interpreter.h
enum stmt {
PRINT = 1,
IF,
LET,
END,
RUN,
LIST
};
enum term {
VAR = 1,
INT
};
typedef struct termNode {
enum term type;
union {
char *var;
int intlit;
} u;
} Term;
typedef struct stmtNode {
int line;
enum stmt type;
struct stmtNode *next;
union {
struct {
char *var;
} print;
struct {
Term *term;
int target;
} ifs;
struct {
char *var;
Term *left;
char op;
Term *right;
} let;
} u;
} Stmt;
typedef struct value {
char *name;
int value;
struct value *next;
} Value;
void run(Stmt *);
void list(Stmt *);
Stmt * input();
Stmt *add(Stmt *head, Stmt *p);
main.c
#include <std.lib>
#include "interpreter.h"
int main(int argc, char *argv[]) {
Stmt *p;
Stmt *head;
head = NULL;
while ((p = input())) {
switch (p->type) {
case RUN:
free(p);
run(head);
break;
case LIST:
free(p);
list(head);
break;
default:
head = add(head, p);
break;
}
}
}
What I'm trying to do is take a string from stdin, parse it to a Stmt structure, and return a pointer to it.
Code:
#include <stdio.h
#include <stdlib.h>
#include <string.h>
#include <main.c>
char *input(){
char *string;
*fgets(char *s, int 100, stdin);
}
so if my input is as follows:
"1 LET a = 1 + 2"
How would I parse for this in c? I read up on a function strtok, that uses a delimiter to split a string into tokens, would that help?