-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import * as compiler from './compiler'; |