main.c 2.1 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
#include "debug.h"
8
#include "desc.h"
9
#include "prim.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
#include "parse.h"
11
#include "code.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
12
#include "mem.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
13

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
15
16
17
18
19
20
21
22
23
24
25
long readfile(char **string, FILE *f) {
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    fseek(f, 0, SEEK_SET);

    *string = (char*) malloc(fsize);
    fread(*string, fsize, 1, f);
    fclose(f);

    return fsize;
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
26
27
int main ( int argc, char *argv[] )
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
28
29
30
    init_mem();
    init_desc();
    init_prim();
31

32
    char* input = "..\\tests\\Braun.bsapl";
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
33
34
35
36
37
38
39
40
    
    if ( argc == 2 )
    {
        input = argv[1];
    }

    FILE* file = fopen(input, "r");
    char* line = NULL;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
    long len = 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
42

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
43
44
45
    if ( file == 0 )
    {
        printf( "Could not open file\n" );
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46
        exit(-1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
47
48
    }
    else 
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
49
50
51
52
    {	        
        len = readfile(&line, file);
                
        if(len < 0)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
53
54
55
56
57
        {
            printf( "No Input\n" );
        }
			
        fclose( file );
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
58
        if(len < 0) exit(-1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
59
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
60

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
61
#ifdef DEBUG
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
62
63
    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
64
#endif
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
65
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
66
67
68
    int nrfuns = parse(&line, len);

#ifdef DEBUG
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
69
    printf("Number of functions parsed: %d\n", nrfuns);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
70
#endif
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
71

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
72
73
74
75
76
    if(nrfuns<=0)
    {
        exit(0);
    }
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
77
    // TODO: put it into a special "expression" space, instead of "code"
78
    char *exprstream = "A0 F4 main";
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
79
80

    Code* expr = parseTerm(&exprstream);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
81
    
82
#ifdef BENCHMARK
83
84
85
    struct timeval t1, t2;
    gettimeofday(&t1, NULL);
#endif
86
            
87
    push_a(NULL);
88
    exec(expr, stack_top_a, stack_top_a);    
89
    Thunk* res = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
90

91
#ifdef BENCHMARK
92
    gettimeofday(&t2, NULL);
93
94
#endif

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
95
96
    print(res, true);

97
#ifdef BENCHMARK
98
99
    // compute and print the elapsed time in millisec
    double elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
100
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms    
101
    printf("\n\nexecution time: %G ms\n", elapsedTime);
102
103
104
#endif
    
#ifdef DEBUG    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
105
    print_stat();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
106
#endif
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
107
108
    
    exit(0);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
109
}