summaryrefslogtreecommitdiff
path: root/src/parser/parser.js
blob: 3434fee2ca0845c03c2b5ea36ed431c2d3652a25 (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
const process = require('node:process');
const fs      = require('node:fs');
const parser  = require('./grammar.js').parser;

parser.yy = {
	pc: 0,

	step: function() { this.pc += 1; },

	equ: {},
	line: {},

	setEqu: function(l, v) {
		this.equ[l] = v;
	},
	setLine: function(l) {
		this.line[l] = this.pc;
	},
	
	getLabel: function(l) {
		if (this.equ[l] !== undefined) {
			return this.equ[l];
		}

		return l;
	},

	tidyAddress: function(pc, addr) {
		if (typeof(addr.value) === "string") {
			addr.value = this.line[addr.value] - pc;
		}
	},
};


exports.assemble = function(sourceCode) {
	let result = parser.parse(sourceCode.toUpperCase());

	// set start point
	if (typeof(result.start) === "string") {
		result.start = parser.yy.line[result.start];
	}

	// convert line labels to actual numbers
	for (let pc=0; pc<result.program.length; pc += 1) {
		parser.yy.tidyAddress(pc, result.program[pc].a);
		parser.yy.tidyAddress(pc, result.program[pc].b);
	}

	return result;
}