Browse Source

adding all the code

nbillard 1 year ago
parent
commit
017af25717
32 changed files with 1003 additions and 0 deletions
  1. 36
    0
      Makefile
  2. 78
    0
      calcop.c
  3. 11
    0
      calcop.h
  4. 93
    0
      cmpop.c
  5. 10
    0
      cmpop.h
  6. 70
    0
      command.c
  7. 17
    0
      command.h
  8. 9
    0
      definitions.h
  9. 38
    0
      exec.c
  10. 9
    0
      exec.h
  11. 76
    0
      functions.c
  12. 13
    0
      functions.h
  13. 33
    0
      lexer.c
  14. 11
    0
      lexer.h
  15. 58
    0
      logic.c
  16. 15
    0
      logic.h
  17. 18
    0
      main.c
  18. 15
    0
      main.h
  19. 62
    0
      num.c
  20. 22
    0
      num.h
  21. 66
    0
      pile.c
  22. 27
    0
      pile.h
  23. 49
    0
      pileInt.c
  24. 25
    0
      pileInt.h
  25. 1
    0
      prog.forth
  26. 29
    0
      show.c
  27. 12
    0
      show.h
  28. 39
    0
      stackops.c
  29. 11
    0
      stackops.h
  30. 5
    0
      state.c
  31. 21
    0
      state.h
  32. 24
    0
      test.sh

+ 36
- 0
Makefile View File

@@ -0,0 +1,36 @@
1
+CC = gcc
2
+CFLAGS = -Wall -pedantic -Werror=format-security -Werror=implicit-function-declaration -D_FORTIFY_SOURCE=2 \
3
+-O2 -pipe -Wl,-z,defs -Wl,-z,now, -Wl,-z,relro
4
+TARGET = main
5
+OBJS = $(TARGET).o logic.o pile.o lexer.o exec.o command.o num.o pileInt.o state.o stackops.o calcop.o \
6
+cmpop.o functions.o show.o
7
+LDFLAGS = -Wall
8
+
9
+default: $(TARGET)
10
+
11
+all: $(TARGET)
12
+
13
+clean:
14
+	rm *.o $(TARGET)
15
+
16
+test: $(TARGET)
17
+	./test.sh
18
+
19
+$(TARGET): $(OBJS)
20
+	$(CC) $(OBJS) -o $@ $(LDFLAGS)
21
+
22
+logic.o: logic.h state.h pileInt.h
23
+pile.o: pile.h num.h
24
+lexer.o: lexer.h
25
+exec.o: exec.h state.h command.h pile.h functions.h
26
+command.o: command.h state.h functions.h calcop.h cmpop.h stackops.h show.h logic.h
27
+state.o: state.h definitions.h pile.h lexer.h
28
+num.o: num.h
29
+show.o: show.h state.h
30
+pileInt.o: pileInt.h definitions.h
31
+calcop.o: calcop.h pile.h state.h num.h
32
+stackops.o: stackops.h state.h
33
+cmpop.o: cmpop.h state.h num.h pile.h
34
+functions.o: functions.h definitions.h state.h pileInt.h
35
+
36
+.PHONY: default all clean test

+ 78
- 0
calcop.c View File

@@ -0,0 +1,78 @@
1
+#include "calcop.h"
2
+
3
+#include "num.h"
4
+#include "pile.h"
5
+
6
+int addi(int i, int j) {
7
+    return i + j;
8
+}
9
+float addf(float i, float j) {
10
+    return i + j;
11
+}
12
+int subi(int i, int j) {
13
+    return i - j;
14
+}
15
+float subf(float i, float j) {
16
+    return i - j;
17
+}
18
+int multi(int i, int j) {
19
+    return i * j;
20
+}
21
+float multf(float i, float j) {
22
+    return i * j;
23
+}
24
+int divi(int i, int j) {
25
+    return i/j;
26
+}
27
+float divf(float i, float j) {
28
+    return i/j;
29
+}
30
+
31
+void calcop( int (*opint)(int,int), float (*opfloat)(float,float), struct Pile* pile ) {
32
+    struct NumContainer res;
33
+    struct NumContainer num1;
34
+    struct NumContainer num2;
35
+    getlastnums(&num1, &num2, pile);
36
+
37
+    if ( num1.t == INT ) {
38
+        if ( num2.t == INT ) {
39
+            res.t = INT;
40
+            res.n.vali = opint( num1.n.vali, num2.n.vali );
41
+        } else {
42
+            res.t = FLOAT;
43
+            res.n.valf = opfloat((float)num1.n.vali, num2.n.valf);
44
+        }
45
+    } else {
46
+        res.t = FLOAT;
47
+        if ( num2.t == INT ) {
48
+            res.n.valf = opfloat(num1.n.valf, (float)num2.n.vali);
49
+        } else {
50
+            res.n.valf = opfloat(num1.n.valf, num2.n.valf);
51
+        }
52
+    }
53
+    push(pile, res);
54
+}
55
+
56
+void add(struct State* state) {
57
+    if (state->mode == EXECUTE) {
58
+        calcop(addi, addf, &(state->pile));
59
+    }
60
+}
61
+
62
+void sub(struct State* state) {
63
+    if (state->mode == EXECUTE) {
64
+        calcop(subi, subf, &(state->pile));
65
+    }
66
+}
67
+
68
+void mult(struct State* state) {
69
+    if (state->mode == EXECUTE) {
70
+        calcop(multi, multf, &(state->pile));
71
+    }
72
+}
73
+
74
+void divide(struct State* state) {
75
+    if (state->mode == EXECUTE) {
76
+        calcop(divi, divf, &(state->pile));
77
+    }
78
+}

+ 11
- 0
calcop.h View File

@@ -0,0 +1,11 @@
1
+#ifndef CALCOP_H_
2
+#define CALCOP_H_
3
+
4
+#include "state.h"
5
+
6
+void add(struct State* state);
7
+void sub(struct State* state);
8
+void mult(struct State* state);
9
+void divide(struct State* state);
10
+
11
+#endif // CALCOP_H_

+ 93
- 0
cmpop.c View File

@@ -0,0 +1,93 @@
1
+#include "cmpop.h"
2
+
3
+#include "num.h"
4
+#include "pile.h"
5
+
6
+
7
+int equali(int i, int j) {
8
+    if ( i == j ) {
9
+        return -1;
10
+    } else {
11
+        return 0;
12
+    }
13
+}
14
+int equalf(float i, float j) {
15
+    if ( i == j ) {
16
+        return -1;
17
+    } else {
18
+        return 0;
19
+    }
20
+}
21
+int lessThani(int i, int j) {
22
+    if ( i < j ) {
23
+        return -1;
24
+    } else {
25
+        return 0;
26
+    }
27
+}
28
+int lessThanf(float i, float j) {
29
+    if ( i < j ) {
30
+        return -1;
31
+    } else {
32
+        return 0;
33
+    }
34
+}
35
+int greaterThani(int i, int j) {
36
+    if ( i > j ) {
37
+        return -1;
38
+    } else {
39
+        return 0;
40
+    }
41
+}
42
+int greaterThanf(float i, float j) {
43
+    if ( i > j ) {
44
+        return -1;
45
+    } else {
46
+        return 0;
47
+    }
48
+}
49
+
50
+void cmpop( int (*opint)(int,int), int (*opfloat)(float,float), struct Pile* pile ) {
51
+    struct NumContainer res;
52
+    struct NumContainer num1;
53
+    struct NumContainer num2;
54
+    getlastnums(&num1, &num2, pile);
55
+    push(pile, num1);
56
+
57
+
58
+    /* getlastnums(&num1, &num2, pile); */
59
+
60
+    res.t = INT;
61
+    if ( num1.t == INT ) {
62
+        if ( num2.t == INT ) {
63
+            res.n.vali = opint( num1.n.vali, num2.n.vali );
64
+        } else {
65
+            res.n.valf = opfloat((float)num1.n.vali, num2.n.valf);
66
+        }
67
+    } else {
68
+        if ( num2.t == INT ) {
69
+            res.n.valf = opfloat(num1.n.valf, (float)num2.n.vali);
70
+        } else {
71
+            res.n.valf = opfloat(num1.n.valf, num2.n.valf);
72
+        }
73
+    }
74
+    push(pile, res);
75
+}
76
+
77
+void equal(struct State* state) {
78
+    if (state->mode == EXECUTE) {
79
+        cmpop(equali, equalf, &state->pile);
80
+    }
81
+}
82
+
83
+void less(struct State* state) {
84
+    if (state->mode == EXECUTE) {
85
+        cmpop(lessThani, lessThanf, &state->pile);
86
+    }
87
+}
88
+
89
+void greater(struct State* state) {
90
+    if (state->mode == EXECUTE) {
91
+        cmpop(greaterThani, greaterThanf, &state->pile);
92
+    }
93
+}

+ 10
- 0
cmpop.h View File

@@ -0,0 +1,10 @@
1
+#ifndef CMPOP_H_
2
+#define CMPOP_H_
3
+
4
+#include "state.h"
5
+
6
+void equal(struct State* state);
7
+void less(struct State* state);
8
+void greater(struct State* state);
9
+
10
+#endif // CMPOP_H_

+ 70
- 0
command.c View File

@@ -0,0 +1,70 @@
1
+#include "command.h"
2
+
3
+#include "functions.h"
4
+#include "calcop.h"
5
+#include "cmpop.h"
6
+#include "stackops.h"
7
+#include "show.h"
8
+#include "logic.h"
9
+
10
+#include <stdlib.h>
11
+#include <string.h>
12
+#include <stdio.h>
13
+
14
+void initCmds( struct CmdList** cmds ) {
15
+    initLogic();
16
+    initFunctions();
17
+    prependCmd(cmds, "DROP", dropCmd);
18
+    prependCmd(cmds, "DUP", dupCmd);
19
+    prependCmd(cmds, "SWAP", swapCmd);
20
+    prependCmd(cmds, "ROT", rotCmd);
21
+    prependCmd(cmds, "CR", crCmd);
22
+    prependCmd(cmds, "IF", ifCmd);
23
+    prependCmd(cmds, "ELSE", elseCmd);
24
+    prependCmd(cmds, "THEN", thenCmd);
25
+    prependCmd(cmds, "BEGIN", beginCmd);
26
+    prependCmd(cmds, "UNTIL", untilCmd);
27
+    prependCmd(cmds, ":", colonCmd);
28
+    prependCmd(cmds, ";", semicolonCmd);
29
+    prependCmd(cmds, "+", add);
30
+    prependCmd(cmds, "-", sub);
31
+    prependCmd(cmds, "*", mult);
32
+    prependCmd(cmds, "/", divide);
33
+    prependCmd(cmds, "=", equal);
34
+    prependCmd(cmds, "<", less);
35
+    prependCmd(cmds, ">", greater);
36
+    prependCmd(cmds, ".", point);
37
+    prependCmd(cmds, ".S", pointS);
38
+    prependCmd(cmds, ".\"", pointQuote);
39
+}
40
+
41
+void prependCmd( struct CmdList** cmds, char* cmdName, void (*func)(struct State*) ) {
42
+    struct CmdList* aux = *cmds;
43
+    *cmds = (struct CmdList*)malloc(sizeof(struct CmdList));
44
+    (*cmds)->next = aux;
45
+    (*cmds)->func = func;
46
+    strncpy((*cmds)->cmdName, cmdName, MAX_CMD_NAME_LENGTH);
47
+    (*cmds)->cmdName[MAX_CMD_NAME_LENGTH-1] = '\0';
48
+}
49
+
50
+void endCmds( struct CmdList** pcmds ) {
51
+    struct CmdList* cmds = *pcmds;
52
+    struct CmdList* aux;
53
+    while (cmds != NULL) {
54
+        aux = cmds;
55
+        cmds = cmds->next;
56
+        free(aux);
57
+    }
58
+    *pcmds = NULL;
59
+}
60
+
61
+int tryCallCmds( struct CmdList* cmds, char* s, struct State* state ) {
62
+    while (cmds != NULL) {
63
+        if ( !strcmp(cmds->cmdName, s) ) {
64
+            cmds->func(state);
65
+            return 1;
66
+        }
67
+        cmds = cmds->next;
68
+    }
69
+    return 0;
70
+}

+ 17
- 0
command.h View File

@@ -0,0 +1,17 @@
1
+#ifndef COMMAND_H_
2
+#define COMMAND_H_
3
+
4
+#include "state.h"
5
+
6
+struct CmdList {
7
+    struct CmdList* next;
8
+    char cmdName[MAX_CMD_NAME_LENGTH];
9
+    void (*func)(struct State*);
10
+};
11
+
12
+void initCmds( struct CmdList** cmds );
13
+void prependCmd( struct CmdList** cmds, char* s, void (*func)(struct State*) );
14
+void endCmds( struct CmdList** cmds );
15
+int tryCallCmds( struct CmdList* cmds, char* s, struct State* state );
16
+
17
+#endif // COMMAND_H_

+ 9
- 0
definitions.h View File

@@ -0,0 +1,9 @@
1
+#ifndef DEFINITIONS_H_
2
+#define DEFINITIONS_H_
3
+
4
+typedef unsigned int programPointer;
5
+#define MAX_CMD_NAME_LENGTH 6
6
+
7
+struct FunctionsList;
8
+
9
+#endif // DEFINITIONS_H_

+ 38
- 0
exec.c View File

@@ -0,0 +1,38 @@
1
+#include "exec.h"
2
+
3
+#include "pile.h"
4
+#include "functions.h"
5
+
6
+#include <stdlib.h>
7
+#include <ctype.h>
8
+#include <stdio.h>
9
+#include <string.h>
10
+
11
+
12
+void execute(struct State *state, struct CmdList* cmds) {
13
+    struct NumContainer num1;
14
+    char* instruction;
15
+    while (state->instructionPointer < (programPointer) state->prog->taille) {
16
+        instruction = getCurrentToken(state);
17
+        if (state->mode == EXECUTE) {
18
+            if ( isdigit(instruction[0]) ) {
19
+                num1 = readNum(instruction);
20
+                push(&(state->pile), num1);
21
+            } else if ( !tryCallCmds( cmds, instruction, state ) ) {
22
+                if (!tryCallFunctions(instruction, state)) {
23
+                    fprintf(stderr, "%s is not a known symbol\n", instruction);
24
+                    exit(1);
25
+                }
26
+            }
27
+        } else if (state->mode == PRINT){
28
+            if (!strncmp(instruction, "\"", 2)) {
29
+                state->mode = EXECUTE;
30
+            } else {
31
+                printf("%s ", instruction);
32
+            }
33
+        } else {
34
+            tryCallCmds(cmds, instruction, state);
35
+        }
36
+        ++(state->instructionPointer);
37
+    }
38
+}

+ 9
- 0
exec.h View File

@@ -0,0 +1,9 @@
1
+#ifndef EXEC_H_
2
+#define EXEC_H_
3
+
4
+#include "state.h"
5
+#include "command.h"
6
+
7
+void execute(struct State* state, struct CmdList* cmds);
8
+
9
+#endif // EXEC_H_

+ 76
- 0
functions.c View File

@@ -0,0 +1,76 @@
1
+#include "functions.h"
2
+
3
+#include "definitions.h"
4
+#include "pileInt.h"
5
+
6
+#include <stdlib.h>
7
+#include <string.h>
8
+
9
+static struct PileInt callStack;
10
+
11
+struct FunctionsList {
12
+    struct FunctionsList* next;
13
+    char* functionName;
14
+    programPointer instructionPointer;
15
+};
16
+
17
+void initFunctions() {
18
+    initPileI(&callStack);
19
+}
20
+
21
+void prependFunction(struct State* state) {
22
+    int functionNameLength;
23
+    struct FunctionsList* aux = state->functions;
24
+    state->functions = (struct FunctionsList*) malloc(sizeof(struct FunctionsList));
25
+    state->functions->next = aux;
26
+    ++(state->instructionPointer);
27
+    functionNameLength = strlen(getCurrentToken(state)) + 1; // counting the '\0'
28
+    state->functions->functionName = (char*)malloc(sizeof(char)*functionNameLength);
29
+    strncpy(state->functions->functionName, getCurrentToken(state), functionNameLength);
30
+    state->functions->instructionPointer = state->instructionPointer;
31
+    state->mode = WAIT_FOR_SEMICOLON;
32
+}
33
+
34
+void endFunctions(struct FunctionsList** pfunctions) {
35
+    struct FunctionsList* functions = *pfunctions;
36
+    struct FunctionsList* aux;
37
+    while (functions != NULL) {
38
+        aux = functions;
39
+        functions = functions->next;
40
+        free(aux->functionName);
41
+        free(aux);
42
+    }
43
+    *pfunctions = NULL;
44
+}
45
+
46
+int tryCallFunctions(char* func, struct State* state) {
47
+    struct FunctionsList* aux = state->functions;
48
+    while (aux != NULL) {
49
+        if (!strcmp(aux->functionName, func)) {
50
+            pushPileI(&callStack, state->instructionPointer);
51
+            state->instructionPointer = aux->instructionPointer;
52
+            return 1;
53
+        }
54
+        aux = aux->next;
55
+    }
56
+    return 0;
57
+}
58
+
59
+void returnFromFunction(struct State* state) {
60
+    state->instructionPointer = topPileI(&callStack);
61
+    popPileI(&callStack);
62
+}
63
+
64
+void colonCmd(struct State* state) {
65
+    if (state->mode == EXECUTE) {
66
+        prependFunction(state);
67
+    }
68
+}
69
+
70
+void semicolonCmd(struct State* state) {
71
+    if (state->mode == WAIT_FOR_SEMICOLON) {
72
+        state->mode = EXECUTE;
73
+    } else if (state->mode == EXECUTE) {
74
+        returnFromFunction(state);
75
+    }
76
+}

+ 13
- 0
functions.h View File

@@ -0,0 +1,13 @@
1
+#ifndef FUNCTIONS_H_
2
+#define FUNCTIONS_H_
3
+
4
+#include "state.h"
5
+
6
+
7
+void initFunctions();
8
+int tryCallFunctions(char* func, struct State* state);
9
+void colonCmd(struct State* state);
10
+void semicolonCmd(struct State* state);
11
+
12
+
13
+#endif // FUNCTIONS_H_

+ 33
- 0
lexer.c View File

@@ -0,0 +1,33 @@
1
+#include "lexer.h"
2
+
3
+#include <stdlib.h>
4
+#include <string.h>
5
+
6
+
7
+int numberOfDelimiters(char* string) {
8
+    int count = 0;
9
+    for (int i = 0; i < (int)strlen(string); ++i) {
10
+        if (string[i] == ' ') {
11
+            ++count;
12
+        }
13
+    }
14
+    return count;
15
+}
16
+
17
+struct Programm* lexer(char* chaine) {
18
+    char *token, *str;
19
+    str = strdup(chaine);
20
+    int i = 0;
21
+    int arraysize = numberOfDelimiters(str) + 1;
22
+    char** programme = (char**)malloc(sizeof(char*)*arraysize);
23
+
24
+    while ((token = strsep(&str, " "))) {
25
+        programme[i] = token;
26
+        ++i;
27
+    }
28
+
29
+    struct Programm* res = (struct Programm*) malloc(sizeof(struct Programm));
30
+    res->tokens = programme;
31
+    res->taille = i;
32
+    return res;
33
+}

+ 11
- 0
lexer.h View File

@@ -0,0 +1,11 @@
1
+#ifndef LEXER_H_
2
+#define LEXER_H_
3
+
4
+struct Programm {
5
+    char** tokens;
6
+    int taille;
7
+};
8
+
9
+struct Programm* lexer(char* chaine);
10
+
11
+#endif // LEXER_H_

+ 58
- 0
logic.c View File

@@ -0,0 +1,58 @@
1
+#include "logic.h"
2
+
3
+#include "pileInt.h"
4
+
5
+static struct PileInt loops;
6
+
7
+void initLogic() {
8
+    initPileI(&loops);
9
+}
10
+
11
+void ifCmd(struct State* state) {
12
+    if (state->mode == EXECUTE) {
13
+        struct NumContainer cond;
14
+        cond = top(&state->pile);
15
+        pop(&state->pile);
16
+        if (cond.t == INT && cond.n.vali == 0) {
17
+            state->mode = IGNORE;
18
+        } else if (cond.t == FLOAT && cond.n.valf == 0.0 ) {
19
+            state->mode = IGNORE;
20
+        }
21
+    }
22
+}
23
+
24
+void elseCmd(struct State* state) {
25
+    if (state->mode == EXECUTE) {
26
+        state->mode = WAIT_FOR_ENDIF;
27
+    } else if (state->mode == IGNORE) {
28
+        state->mode = EXECUTE;
29
+    }
30
+}
31
+
32
+void thenCmd(struct State* state) {
33
+    if (state->mode == IGNORE || state->mode == WAIT_FOR_ENDIF) {
34
+        state->mode = EXECUTE;
35
+    }
36
+}
37
+
38
+void beginCmd(struct State* state) {
39
+    if (state->mode == EXECUTE) {
40
+        if (evalasBool(top(&state->pile))) {
41
+            pushPileI(&loops, state->instructionPointer);
42
+        } else {
43
+            state->mode = WAIT_FOR_UNTIL;
44
+        }
45
+    }
46
+}
47
+
48
+void untilCmd(struct State* state) {
49
+    if (state->mode == WAIT_FOR_UNTIL) {
50
+        state->mode = EXECUTE;
51
+    } else if (state->mode == EXECUTE) {
52
+        if (evalasBool(top(&state->pile))) {
53
+            state->instructionPointer = topPileI(&loops);
54
+        } else {
55
+            popPileI(&loops);
56
+        }
57
+    }
58
+}

+ 15
- 0
logic.h View File

@@ -0,0 +1,15 @@
1
+#ifndef LOGIC_H_
2
+#define LOGIC_H_
3
+
4
+#include "state.h"
5
+
6
+void initLogic();
7
+void ifCmd(struct State* state);
8
+void elseCmd(struct State* state);
9
+void thenCmd(struct State* state);
10
+void beginCmd(struct State* state);
11
+void untilCmd(struct State* state);
12
+
13
+
14
+
15
+#endif // LOGIC_H_

+ 18
- 0
main.c View File

@@ -0,0 +1,18 @@
1
+#include "main.h"
2
+
3
+int main(int argc, char* argv[]) {
4
+    struct State state;
5
+    struct CmdList* cmds;
6
+    if ( argc != 2 ) {
7
+        fprintf(stderr, "Usage: ./main 'instructions'\n");
8
+        exit(1);
9
+    }
10
+
11
+    initCmds(&cmds);
12
+    state.prog = lexer(argv[1]);
13
+    state.instructionPointer = 0;
14
+    init(&(state.pile));
15
+    state.mode = EXECUTE;
16
+    execute(&state, cmds);
17
+    endCmds(&cmds);
18
+}

+ 15
- 0
main.h View File

@@ -0,0 +1,15 @@
1
+#ifndef MAIN_H_
2
+#define MAIN_H_
3
+
4
+#include <stdio.h>
5
+#include <stdlib.h>
6
+#include <string.h>
7
+#include <ctype.h>
8
+
9
+#include "pile.h"
10
+#include "lexer.h"
11
+#include "exec.h"
12
+
13
+
14
+
15
+#endif // MAIN_H_

+ 62
- 0
num.c View File

@@ -0,0 +1,62 @@
1
+#include "num.h"
2
+
3
+#include <stdio.h>
4
+#include <stdlib.h>
5
+#include <ctype.h>
6
+
7
+void printLnNum( struct NumContainer num ) {
8
+    if ( num.t == INT ) {
9
+        printf("%d\n", num.n.vali);
10
+    } else {
11
+        printf("%f\n", num.n.valf);
12
+    }
13
+}
14
+
15
+void printNum( struct NumContainer num ) {
16
+    if ( num.t == INT ) {
17
+        printf("%d ", num.n.vali);
18
+    } else {
19
+        printf("%f ", num.n.valf);
20
+    }
21
+}
22
+
23
+char evalasBool( struct NumContainer num ) {
24
+    if (num.t == INT) {
25
+        if (num.n.vali == 0) {
26
+            return 0;
27
+        } else {
28
+            return 1;
29
+        }
30
+    } else {
31
+        if (num.n.valf == 0.0) {
32
+            return 0;
33
+        } else {
34
+            return 1;
35
+        }
36
+    }
37
+}
38
+
39
+struct NumContainer readNum(char* s) {
40
+    char* original = s;
41
+    int is_int = 1;
42
+    struct NumContainer res;
43
+    while (*s != '\0') {
44
+        if ( !isdigit(*s) ) {
45
+            if ( *s == '.' && is_int ) {
46
+                is_int = 0;
47
+            } else {
48
+                fprintf(stderr, "'%s' is not a number", original);
49
+                exit(1);
50
+            }
51
+        }
52
+        ++s;
53
+    }
54
+    if (is_int) {
55
+        res.t = INT;
56
+        res.n.vali = atoi(original);
57
+    } else {
58
+        res.t = FLOAT;
59
+        res.n.valf = atof(original);
60
+    }
61
+    return res;
62
+}

+ 22
- 0
num.h View File

@@ -0,0 +1,22 @@
1
+#ifndef NUM_H_
2
+#define NUM_H_
3
+
4
+enum NUMTYPE { INT, FLOAT };
5
+
6
+union Num {
7
+    int vali;
8
+    float valf;
9
+};
10
+
11
+struct NumContainer {
12
+    enum NUMTYPE t;
13
+    union Num n;
14
+};
15
+
16
+void printNum( struct NumContainer num );
17
+void printLnNum( struct NumContainer num );
18
+char evalasBool( struct NumContainer num );
19
+struct NumContainer readNum(char* s);
20
+
21
+
22
+#endif // NUM_H_

+ 66
- 0
pile.c View File

@@ -0,0 +1,66 @@
1
+#include "pile.h"
2
+
3
+#include <stdlib.h>
4
+#include <stdio.h>
5
+
6
+void init( struct Pile* p ) {
7
+    p->height = 0;
8
+    p->l = NULL;
9
+}
10
+
11
+void push( struct Pile* p, struct NumContainer i ) {
12
+    struct List* aux = p->l;
13
+
14
+    p->l = (struct List*)malloc(sizeof(struct List));
15
+    if (p->l == NULL) {
16
+        fprintf(stderr, "Error: Could not allocate memory for the next cell on the stack\n");
17
+        return;
18
+    }
19
+
20
+    p->l->next = aux;
21
+    p->l->i = i;
22
+    ++(p->height);
23
+}
24
+
25
+struct NumContainer top( struct Pile* p ) {
26
+    if ( p->height == 0 ) {
27
+        fprintf(stderr, "Error: The stack has no element, cannot find top\n");
28
+        exit(1);
29
+    }
30
+    return p->l->i;
31
+}
32
+
33
+void print( struct Pile* p ) {
34
+    struct List* aux = p->l;
35
+
36
+    while (aux != NULL) {
37
+        printNum(aux->i);
38
+        printf(" ");
39
+        aux = aux->next;
40
+    }
41
+    /* printf("NULL"); */
42
+}
43
+
44
+void pop( struct Pile* p ) {
45
+    struct List* aux;
46
+
47
+    if ( p->height != 0 ) {
48
+        --(p->height);
49
+        aux = p->l;
50
+        p->l = p->l->next;
51
+        free(aux);
52
+    }
53
+}
54
+
55
+void end( struct Pile* p ) {
56
+    while (p->height != 0) {
57
+        pop(p);
58
+    }
59
+}
60
+
61
+void getlastnums( struct NumContainer* i, struct NumContainer* j, struct Pile* pile ) {
62
+    *j = top(pile);
63
+    pop(pile);
64
+    *i = top(pile);
65
+    pop(pile);
66
+}

+ 27
- 0
pile.h View File

@@ -0,0 +1,27 @@
1
+#ifndef PILE_H_
2
+#define PILE_H_
3
+
4
+#include "num.h"
5
+
6
+struct List {
7
+    struct NumContainer i;
8
+    struct List* next;
9
+};
10
+
11
+struct Pile {
12
+    struct List* l;
13
+    unsigned int height;
14
+};
15
+
16
+void init( struct Pile* p );
17
+void push( struct Pile* p, struct NumContainer i );
18
+struct NumContainer top( struct Pile* p );
19
+void print( struct Pile* p );
20
+void pop( struct Pile* p );
21
+void end( struct Pile* p );
22
+void getlastnums( struct NumContainer* i, struct NumContainer* j, struct Pile* pile);
23
+
24
+
25
+
26
+
27
+#endif // PILE_H_

+ 49
- 0
pileInt.c View File

@@ -0,0 +1,49 @@
1
+#include "pileInt.h"
2
+
3
+#include <stdlib.h>
4
+#include <stdio.h>
5
+
6
+void initPileI( struct PileInt* p ) {
7
+    p->height = 0;
8
+    p->l = NULL;
9
+}
10
+
11
+void pushPileI( struct PileInt* p, programPointer i ) {
12
+    struct ListInt* aux = p->l;
13
+
14
+    p->l = (struct ListInt*)malloc(sizeof(struct ListInt));
15
+    if (p->l == NULL) {
16
+        fprintf(stderr, "Error: Could not allocate memory for the next cell on the stack");
17
+        return;
18
+    }
19
+
20
+    p->l->next = aux;
21
+    p->l->i = i;
22
+    ++(p->height);
23
+}
24
+
25
+int topPileI( struct PileInt* p ) {
26
+    if ( p->height == 0 ) {
27
+        fprintf(stderr, "Error: The stack has no element, cannot find top");
28
+        exit(1);
29
+    }
30
+    return p->l->i;
31
+}
32
+
33
+void popPileI( struct PileInt* p ) {
34
+    struct ListInt* aux;
35
+
36
+    if ( p->height != 0 ) {
37
+        --(p->height);
38
+        aux = p->l;
39
+        p->l = p->l->next;
40
+        free(aux);
41
+    }
42
+}
43
+
44
+void endPileI( struct PileInt* p ) {
45
+    while (p->height != 0) {
46
+        popPileI(p);
47
+    }
48
+}
49
+

+ 25
- 0
pileInt.h View File

@@ -0,0 +1,25 @@
1
+#ifndef PILE_INT_H_
2
+#define PILE_INT_H_
3
+
4
+#include "definitions.h"
5
+
6
+struct ListInt {
7
+    programPointer i;
8
+    struct ListInt* next;
9
+};
10
+
11
+struct PileInt {
12
+    struct ListInt* l;
13
+    unsigned int height;
14
+};
15
+
16
+void initPileI( struct PileInt* p );
17
+void pushPileI( struct PileInt* p, programPointer i );
18
+int topPileI( struct PileInt* p );
19
+void popPileI( struct PileInt* p );
20
+void endPileI( struct PileInt* p );
21
+
22
+
23
+
24
+
25
+#endif // PILE_H_

+ 1
- 0
prog.forth View File

@@ -0,0 +1 @@
1
+3 87 +

+ 29
- 0
show.c View File

@@ -0,0 +1,29 @@
1
+#include "show.h"
2
+
3
+#include <stdio.h>
4
+
5
+void point(struct State* state) {
6
+    if (state->mode == EXECUTE) {
7
+        struct NumContainer num = top(&state->pile);
8
+        pop(&state->pile);
9
+        printNum(num);
10
+    }
11
+}
12
+
13
+void pointS(struct State* state) {
14
+    if (state->mode == EXECUTE) {
15
+        print(&state->pile);
16
+    }
17
+}
18
+
19
+void pointQuote(struct State* state) {
20
+    if (state->mode == EXECUTE) {
21
+        state->mode = PRINT;
22
+    }
23
+}
24
+
25
+void crCmd(struct State* state) {
26
+    if (state->mode == EXECUTE) {
27
+        printf("\n");
28
+    }
29
+}

+ 12
- 0
show.h View File

@@ -0,0 +1,12 @@
1
+#ifndef SHOW_H_
2
+#define SHOW_H_
3
+
4
+#include "state.h"
5
+
6
+void point(struct State* state);
7
+void pointS(struct State* state);
8
+void pointQuote(struct State* state);
9
+void crCmd(struct State* state);
10
+
11
+
12
+#endif // SHOW_H_

+ 39
- 0
stackops.c View File

@@ -0,0 +1,39 @@
1
+#include "stackops.h"
2
+
3
+void dropCmd(struct State* state) {
4
+    if (state->mode == EXECUTE) {
5
+        pop(&(state->pile));
6
+    }
7
+}
8
+
9
+void dupCmd(struct State* state) {
10
+    if (state->mode == EXECUTE) {
11
+        push(&(state->pile), top(&(state->pile)));
12
+    }
13
+}
14
+
15
+void swapCmd(struct State* state) {
16
+    if (state->mode == EXECUTE) {
17
+        struct NumContainer num1;
18
+        struct NumContainer num2;
19
+
20
+        getlastnums(&num1, &num2, &(state->pile));
21
+        push(&(state->pile), num2);
22
+        push(&(state->pile), num1);
23
+    }
24
+}
25
+
26
+void rotCmd(struct State* state) {
27
+    if (state->mode == EXECUTE) {
28
+        struct NumContainer num1;
29
+        struct NumContainer num2;
30
+        struct NumContainer num3;
31
+
32
+        getlastnums(&num1, &num2, &(state->pile));
33
+        num3 = top(&(state->pile));
34
+        pop(&(state->pile));
35
+        push(&(state->pile), num2);
36
+        push(&(state->pile), num1);
37
+        push(&(state->pile), num3);
38
+    }
39
+}

+ 11
- 0
stackops.h View File

@@ -0,0 +1,11 @@
1
+#ifndef STACKOPS_H_
2
+#define STACKOPS_H_
3
+
4
+#include "state.h"
5
+
6
+void dropCmd(struct State* state);
7
+void dupCmd(struct State* state);
8
+void swapCmd(struct State* state);
9
+void rotCmd(struct State* state);
10
+
11
+#endif // STACKOPS_H_

+ 5
- 0
state.c View File

@@ -0,0 +1,5 @@
1
+#include "state.h"
2
+
3
+char* getCurrentToken(struct State* state) {
4
+    return state->prog->tokens[state->instructionPointer];
5
+}

+ 21
- 0
state.h View File

@@ -0,0 +1,21 @@
1
+#ifndef STATE_H_
2
+#define STATE_H_
3
+
4
+#include "pile.h"
5
+#include "lexer.h"
6
+#include "definitions.h"
7
+
8
+enum MODE {EXECUTE, PRINT, IGNORE, WAIT_FOR_ENDIF, WAIT_FOR_UNTIL, WAIT_FOR_SEMICOLON};
9
+
10
+struct State {
11
+    struct Pile pile;
12
+    struct Programm* prog;
13
+    enum MODE mode;
14
+    programPointer instructionPointer;
15
+    struct FunctionsList* functions;
16
+};
17
+
18
+char* getCurrentToken(struct State* state);
19
+
20
+
21
+#endif // STATE_H_

+ 24
- 0
test.sh View File

@@ -0,0 +1,24 @@
1
+#!/usr/bin/env bash
2
+
3
+set -x
4
+./main '3 5 + . CR'
5
+./main '3 5 + 4 2 - .S CR'
6
+./main '3 5 + 4 2 - * . CR'
7
+./main '1 +'
8
+./main '1 2 + DUP * . CR'
9
+./main '1 2 DROP . CR'
10
+./main '2 3 SWAP - . CR'
11
+./main '3 2 1 ROT .S CR'
12
+./main '." Bonjour tout le monde " CR'
13
+./main '." hello world 3 DUP pouet " CR'
14
+./main '1 2 . . CR'
15
+./main '1 2 3 .S CR DROP .S CR DROP .S CR'
16
+./main '." le carré de trois est " 3 DUP * . CR'
17
+./main '19 DUP 18 < IF ." mineur " ELSE 18 = IF ." 18 ans " ELSE ." majeur " THEN THEN CR'
18
+./main '12 DUP 18 < IF ." mineur " ELSE 18 = IF ." 18 ans " ELSE ." majeur " THEN THEN CR'
19
+./main '18 DUP 18 < IF ." mineur " ELSE 18 = IF ." 18 ans " ELSE ." majeur " THEN THEN CR'
20
+./main '10 DUP BEGIN SWAP DUP BEGIN ." * " 1 - UNTIL DROP CR SWAP 1 - UNTIL'
21
+./main ': CARRE DUP * ; : CUBE DUP CARRE * ; ." Le cube de 5 est " 5 CUBE . CR'
22
+./main ': MAJEUR DUP 18 < IF ." mineur " ELSE 18 = IF ." 18 ans " ELSE ." majeur " THEN THEN ; 2 MAJEUR CR'
23
+./main ': ETOILE ." * " ; : LIGNE DUP BEGIN ETOILE 1 - UNTIL DROP ; : TRIANGLE BEGIN LIGNE 1 - CR UNTIL ; 10 TRIANGLE'
24
+set +x

Loading…
Cancel
Save