main.c 1.79 KB
Newer Older
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
1
#include <stdio.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
2
#include <stdlib.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
3
#include <stdbool.h>
4
#include <string.h>
5
#include <sys/time.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
6

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
7
8
#include "getline.h"

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
9
#include "debug.h"
10
#include "desc.h"
11
#include "prim.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
12
#include "parse.h"
13
#include "code.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
#include "mem.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
15

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
16
17
int main ( int argc, char *argv[] )
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
18
19
20
    init_mem();
    init_desc();
    init_prim();
21

22
    char* input = "..\\tests\\nfib.bsapl";
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
24
25
26
27
28
29
30
31
32
    
    if ( argc == 2 )
    {
        input = argv[1];
    }

    FILE* file = fopen(input, "r");
    char* line = NULL;
    size_t bufsize = 0;
    size_t len = 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
33

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    if ( file == 0 )
    {
        printf( "Could not open file\n" );
    }
    else 
    {	
        len = getline(&line, &bufsize, file);
        if(len == -1)
        {
            printf( "No Input\n" );
        }
			
        fclose( file );
        if(len == -1) exit(-1);
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
49

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
50
#ifdef DEBUG
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
51
52
    printf("sizeof(int): %d, sizeof(long): %d, sizeof(void*): %d, sizeof(Thunk): %d\n\n", 
            sizeof(int), sizeof(long), sizeof(void*), sizeof(Thunk));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
53
#endif
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
54
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
55
56
57
    int nrfuns = parse(&line, len);

#ifdef DEBUG
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
58
    printf("Number of functions parsed: %d\n", nrfuns);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
59
#endif
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
60
61

    // TODO: put it into a special "expression" space, instead of "code"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
62
    char *exprstream = "VF4 main";
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
63
64

    Code* expr = parseTerm(&exprstream);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
65
    
66
67
68
69
70
#ifdef DEBUG
    struct timeval t1, t2;
    gettimeofday(&t1, NULL);
#endif
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
71
72
    exec(expr, stack_top_a, stack_top_a, NULL, true);
    Thunk* res = eval(pop_a());
73
74
75
76
77
    
#ifdef DEBUG
    gettimeofday(&t2, NULL);
#endif    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
78
79
    print(res, true);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
80
#ifdef DEBUG
81
82
83
84
85
    // compute and print the elapsed time in millisec
    double elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    
    printf("\n\nexecution time: %G ms\n", elapsedTime);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
86
    print_stat();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
87
#endif
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
88
}