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
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cg.h"
#include "comment.h"
#include "encode.h"
#include "error.h"
#include "expected.h"
#include "lexer.h"
#include "parsedirective.h"
#include "parseexpr.h"
#include "parseinstruction.h"
#include "symtab.h"
#include <utils/printcode.h>

void
usage(const char *prg)
{
    fprintf(stderr, "usage: %s [-o output] input\n", prg);
    exit(1);
}

int
main(int argc, char **argv)
{
    if (argc != 2 && argc != 4) {
        usage(argv[0]);
    }

    const char *input = 0, *output = "a.out";

    for (int i = 1; i < argc; ++i) {
        if (!strcmp(argv[i], "-o")) {
            output = argv[++i];
            continue;
        }
        if (!strcmp(argv[i], "--")) {
            continue;
        } else {
            input = argv[i];
            continue;
        }
        if (*argv[i] == '-') {
            usage(argv[0]);
        }
    }

    FILE *in = input ? fopen(input, "r") : stdin;
    FILE *out = fopen(output, "w");

    if (!in) {
        fprintf(stderr, "can not open input file '%s'\n", argv[1]);
        return 1;
    }

    if (!out) {
        fprintf(stderr, "can not open output file '%s'\n", output);
        return 1;
    }

    setLexerIn(in, input);

    getToken();
    while (token.kind != EOI) {
        if (token.kind == UNKNOWN_TOKEN) {
            error("bad token\n");
        }

        // skip empty lines
        if (token.kind == EOL) {
            getToken();
            continue;
        }

        expectedOneOf(2, EMPTY_LABEL, IDENT);
        if (token.kind == IDENT) {
            const struct UStr *label = makeUStr(token.val.cstr);
            cgSetLabel(token.loc, label);
            getToken();
            if (token.kind == COLON) {
                getToken();
            }
        } else {
            getToken();
        }

        if (token.kind == UNKNOWN_TOKEN) {
            error("bad token\n");
        }

        if (!parseInstruction() && !parseDirective()) {
            expected(EOL);
        }
    }
    cgPrint(out);
    symtabPrint(out);
    encodePrintFixups(out);

    fclose(in);
    fclose(out);
}