aboutsummaryrefslogtreecommitdiffgithub
path: root/novice/assembler/index.ts
blob: 452acc710e6c48a073038877c75c2340fb1be1fb (plain)
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
import { getIsa, Isa, isas } from '../isa';
import { Assembler, AssemblerConfig } from './assembler';
import { BaseMachineCodeGenerator, MachineCodeGenerator } from './codegen';
import { configs } from './configs';
import { opSpecs, PseudoOpSpec } from './opspec';
import { Parser, parsers } from './parsers';
import { Serializer, serializers } from './serializers';

function getParser(parserName: string, isa: Isa): Parser {
    if (!parsers.hasOwnProperty(parserName)) {
        throw new Error(`no such parser \`${parserName}'\n`);
    }

    return new parsers[parserName](isa);
}

function getGenerator(): MachineCodeGenerator {
    // Go ahead and return only this lil fella for now
    return new BaseMachineCodeGenerator();
}

function getOpSpec(opSpecName: string): PseudoOpSpec {
    if (!opSpecs.hasOwnProperty(opSpecName)) {
        throw new Error(`no such opspec \`${opSpecName}'\n`);
    }

    return opSpecs[opSpecName];
}

function getSerializer(serializerName: string): Serializer {
    if (!serializers.hasOwnProperty(serializerName)) {
        throw new Error(`no such serializer \`${serializerName}'\n`);
    }

    return new serializers[serializerName]();
}

function getConfig(configName: string): AssemblerConfig {
    if (!configs.hasOwnProperty(configName)) {
        throw new Error(`no such assembler config \`${configName}'\n`);
    }

    const configNames = configs[configName];
    const isa = getIsa(configNames.isa);

    return {
        parser: getParser(configNames.parser, isa),
        generator: getGenerator(),
        isa,
        opSpec: getOpSpec(configNames.opSpec),
        serializer: getSerializer(configNames.serializer),
    };
}

export { Assembler, AssemblerConfig, Serializer, getParser, getGenerator,
         getOpSpec, getSerializer, getConfig };