aboutsummaryrefslogtreecommitdiff
path: root/compiler/fuspelc.c
blob: 99643494643d888136bf149d17e91e7cbfd054aa (plain) (blame)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "error.h"
#include "lex.h"
#include "parse.h"
#include "eval.h"
#include "print.h"

//static char* program =
//	"take_one [x:_] = [x:[]];"
//	"inflist = [1:inflist];"
//	"main = take_one inflist;";
//static char* program =
//	"append []     ys = ys;"
//	"append [x:xs] ys = [x:append xs ys];"
//	"main = append [1:[2:[3:[]]]] [5:[6:[]]];";
static char* program =
	"singleton x = [x:[]];"
	"push x y = [x:y];"
	"is_zero 0 = 1;"
	"is_zero _ = 0;"
	"flip (a,b) = (b,a);"
	"main = push 3 (singleton (flip (is_zero 5, is_zero 0)));";
//static char* program =
//	"trip x y z = (x,(y,z));"
//	"main       = trip (trip 1 2 3) 10 (trip a b c);";
//static char* program =
//	"return   = (n,1);"
//	"main     = return;";

int main(void) {
	token_list* tokens = lex(program);
	
	if (!tokens)
		error(10, "Couldn't lex program.");
	
	printf("\n\nLexed program:\n");
	print_token_list(tokens);
	printf("\n");
	
	fuspel* pgm = parse(tokens);
	free_token_list(tokens);
	free(tokens);
	
	if (!pgm)
		error(10, "Couldn't parse program.");

	printf("\nParsed program:\n");
	print_fuspel(pgm);
	printf("\n\n\n");

	expression to_eval, *evaled;

	to_eval.kind = EXPR_NAME;
	to_eval.var1 = malloc(5);
	if (!to_eval.var1)
		error_no_mem();
	strcpy(to_eval.var1, "main");

	evaled = eval(pgm, &to_eval);
	free_expression(&to_eval);
	if (evaled) {
		print_expression(evaled);
		printf("\n");

		free_expression(evaled);
		free(evaled);
	}

	free_fuspel(pgm);
	free(pgm);

	return 0;
}