-
Notifications
You must be signed in to change notification settings - Fork 1
/
asmproc2.ts
135 lines (101 loc) · 3.05 KB
/
asmproc2.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
// this is an attempt to convert all the grammar in nearley
import fs from "fs";
import commandLineArgs, { OptionDefinition } from 'command-line-args';
import { target, Jump, MOD } from "./cross";
import { nodeToString } from "./nodes";
function parseOptions(optionDefinitions: OptionDefinition[]) {
try {
return commandLineArgs(optionDefinitions);
} catch(ex) {
console.log(ex.message);
process.exit(-1);
}
}
let defines: string[];
main();
function main()
{
const options = parseOptions([
{ name: 'input', alias: 'i', type: String },
{ name: 'output', alias: 'o', type: String },
{ name: 'target', alias: 't', type: String, defaultValue: 'dasm' },
{ name: 'define', alias: 'd', type: String }
]);
if(options === undefined || options.input === undefined || options.output === undefined)
{
console.log("Usage: asmproc -i <inputfile> -o <outputfile>");
process.exit(-1);
return;
}
// set target
target.dasm = options.target === "dasm";
target.ca65 = options.target === "ca65";
target.z80asm = options.target === "z80asm";
target.cpu6502 = target.dasm || target.ca65;
target.cpuz80 = target.z80asm;
defines = options.define === undefined ? [] : options.define.split(",");
let FName = options.input;
let FOut = options.output;
if(FName == FOut)
{
console.log("file names must be different");
process.exit(0);
}
if(!fs.existsSync(FName))
{
console.log("can't find file");
process.exit(0);
}
const input = fs.readFileSync(FName).toString().replace(/\r/g," ");
const output = ProcessFile(input);
console.log(output);
//L.SaveToFile(FOut);
console.log(`asmproc OK, created: "${FOut}"`);
process.exit(0);
}
function ProcessFile(input: string): string
{
let ast = MakeAST(input);
ast = simplifyAST(ast);
// if(ast.type === "error") return `Error in line ${ast.error.location.start.line} column ${ast.error.location.start.column}:\n${ast.error.message}`;
console.log(JSON.stringify(ast,undefined,2));
const compiled = AstToDasm(ast);
return compiled;
}
import { ASTNode } from "./nodes";
function MakeAST(input: string): ASTNode {
const parser = require("./grammar");
const tracer = {
trace: function(d: any) {
//console.log(`${d.type} => ${d.rule}`);
}
};
const options = { tracer };
try
{
const result = parser.parse(input, options) as ASTNode;
return result;
}
catch(ex) {
return { type: "error", error: ex };
}
}
function simplifyAST(ast: ASTNode): ASTNode
{
return ast;
}
// import { BasicSolver } from "./basic";
function AstToDasm(node: ASTNode): string {
return nodeToString(node);
/*
if(node.type === "program") {
return node.items.map(e=>AstToDasm(e)).join("");
}
if(node.type === "basic") {
const solved = BasicSolver(node);
return AstToDasm(solved);
}
throw `node ${node.type} not implemented`;
*/
}