Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Nov 19, 2024
1 parent 923004b commit 9db690a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/ecaCompiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as y3 from 'y3-helper';
import * as vscode from 'vscode';

export class Event {
constructor(private json: y3.json.JObject) { }
}

export class Action {
name: string;
args: Exp[] = [];
constructor(private json: y3.json.JObject) {
this.name = json.action_type as string;
for (let arg of json.args_list as y3.json.JObject[]) {
this.args.push(new Exp(arg));
}
}

make(): string {
return `${this.name}(${this.args.map((arg) => arg.make()).join(', ')})`;
}
}

export class Exp {
name: string;
type: number;
args: Exp[] = [];
constructor(private json: y3.json.JObject) {
this.name = json.sub_type as string;
this.type = json.arg_type as number;
for (let arg of json.args_list as y3.json.JObject[]) {
this.args.push(new Exp(arg));
}
}

make(): string {
return `${this.name}(${this.args.map((arg) => arg.make()).join(', ')})`;
}
}

export class ECA {
name: string;
events: Event[] = [];
actions: Action[] = [];
constructor(private json: y3.json.JObject) {
this.name = json.trigger_name as string;
for (let event of json.event as y3.json.JObject[]) {
this.events.push(new Event(event));
}
for (let action of json.action as y3.json.JObject[]) {
this.actions.push(new Action(action));
}
}

private makeActionPart(): string {
return this.actions.map((action) => action.make()).join('\n ');
}

make(): string {
return this.makeActionPart();
}
}

export class Compiler {
public async compile(input: string | vscode.Uri | y3.json.JObject): Promise<ECA> {
let json: y3.json.JObject;
if (typeof input === 'string') {
json = y3.json.parse(input);
} else if (input instanceof vscode.Uri) {
let file = await y3.fs.readFile(input.fsPath);
y3.assert(file, 'File not found: ' + input.fsPath);
json = y3.json.parse(file.string);
} else {
json = input;
}
return new ECA(json);
}
}
1 change: 1 addition & 0 deletions src/ecaCompiler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import * as compiler from './compiler';

0 comments on commit 9db690a

Please sign in to comment.