aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.c
blob: 4d1f8f97850704c9846546cb1a36dad0640ccf24 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "interpreter.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static void env_update(Env **env, const char *name, int val) {
  for (Env *e = *env; e; e = e->next) {
    if (strcmp(e->name, name) == 0) {
      e->val = val;
      return;
    }
  }
  Env *e = malloc(sizeof(Env));
  e->name = strdup(name);
  e->val = val;
  e->next = *env;
  *env = e;
}

static int env_lookup(Env **env, const char *name) {
  for (Env *e = *env; e; e = e->next) {
    if (strcmp(e->name, name) == 0)
      return e->val;
  }
  env_update(env, name, 0);
  return env_lookup(env, name);
}

void env_print(Env *env) {
  Env *e = env;
  while (e) {
    printf("%s = %d\n", e->name, e->val);
    e = e->next;
  }
}

static int eval_aexpr(Env **env, ASTNode *node) {
  switch (node->type) {
    case NT_INT: return node->u.d_int.val;
    case NT_VAR: return env_lookup(env, node->u.d_var.name);
    case NT_AOP: {
      int aexp1 = eval_aexpr(env, node->u.d_aop.aexp1);
      int aexp2 = eval_aexpr(env, node->u.d_aop.aexp2);
      switch (node->u.d_aop.aop) {
        case AOP_ADD: return aexp1 + aexp2;
        case AOP_SUB: return aexp1 - aexp2;
        case AOP_MUL: return aexp1 * aexp2;
      }
    }
    default:
      fprintf(stderr, "Bad aexpr node %d\n", node->type);
      exit(EXIT_FAILURE);
  }
}

static int eval_bexpr(Env **env, ASTNode *node) {
  switch (node->type) {
    case NT_BOP: {
      int bexp1 = eval_bexpr(env, node->u.d_bop.bexp1);
      int bexp2 = eval_bexpr(env, node->u.d_bop.bexp2);
      switch (node->u.d_bop.bop) {
        case BOP_AND: return bexp1 && bexp2;
        case BOP_OR:  return bexp1 || bexp2;
      }
    }
    case NT_NOT:
      return !eval_bexpr(env, node->u.d_not.bexp);
    case NT_ROP: {
      int aexp1 = eval_aexpr(env, node->u.d_rop.aexp1);
      int aexp2 = eval_aexpr(env, node->u.d_rop.aexp2);
      switch (node->u.d_rop.rop) {
        case ROP_EQ: return aexp1 == aexp2;
        case ROP_NE: return aexp1 != aexp2;
        case ROP_LT: return aexp1 < aexp2;
        case ROP_LE: return aexp1 <= aexp2;
        case ROP_GT: return aexp1 > aexp2;
        case ROP_GE: return aexp1 >= aexp2;
      }
    }
    default:
      fprintf(stderr, "Bad bexpr node %d\n", node->type);
      exit(EXIT_FAILURE);
  }
}

void exec_stmt(Env **env, ASTNode *node) {
  while (node) {
    switch (node->type) {
      case NT_SKIP:
        return;
      case NT_ASSIGN: {
        char *var = node->u.d_assign.var->u.d_var.name;
        int val = eval_aexpr(env, node->u.d_assign.aexp);
        env_update(env, var, val);
        return;
      }
      case NT_SEQ:
        exec_stmt(env, node->u.d_seq.stm1);
        exec_stmt(env, node->u.d_seq.stm2);
        return;
      case NT_IF:
        if (eval_bexpr(env, node->u.d_if.bexp))
          exec_stmt(env, node->u.d_if.stm1);
        else
          exec_stmt(env, node->u.d_if.stm2);
        return;
      case NT_WHILE:
        while (eval_bexpr(env, node->u.d_while.bexp)) {
          exec_stmt(env, node->u.d_while.stm);
        }
        return;
      default:
        fprintf(stderr, "Bad stmt node %d\n", node->type);
        exit(EXIT_FAILURE);
    }
  }
}