Skip to content

WebGAL CodeGen Support #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions YukimiScript.CodeGen/WebGAL.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module YukimiScript.CodeGen.WebGAL

open YukimiScript.Parser
open YukimiScript.Parser.ParserMonad
open YukimiScript.Parser.Elements
open System.Text
open System


let private generateCommand externs (sb: StringBuilder) cmd =
let param =
List.zip
(Map.find (cmd: IntermediateCommandCall).Callee externs
|> List.map (fun x -> x.Parameter))
cmd.Arguments
|> List.filter (fun (_, x) -> x <> Symbol "null")
sb.Append(cmd.Callee).Append(":") |> ignore

let writeArg = function
| String a -> sb.Append(a) |> ignore
| Real a -> sb.Append(a) |> ignore
| Integer a -> sb.Append(a) |> ignore
| Symbol a -> sb.Append(a) |> ignore

let writeParams =
List.iter (fun (p: string, a) ->
sb.Append(" -").Append(p).Append("=") |> ignore
writeArg a)

match param with
| [] -> ()
| ("content", contentArg) :: nextParams ->
writeArg contentArg
writeParams nextParams
| nextParams ->
writeParams nextParams

sb.AppendLine(";") |> ignore


let private generateScene externs sb (scene: IntermediateScene) =
(sb: StringBuilder).Append("label:").Append(scene.Name).AppendLine(";") |> ignore
scene.Block |> List.iter (generateCommand externs sb)
sb.AppendLine() |> ignore


let generateWebGAL dom (Intermediate scenes) =
let externs =
dom.Externs
|> Seq.map (fun (ExternCommand (a, p), _, _) -> a, p)
|> Map.ofSeq

let sb = StringBuilder ()
scenes |> Seq.iter (generateScene externs sb)
sb


1 change: 1 addition & 0 deletions YukimiScript.CodeGen/YukimiScript.CodeGen.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Compile Include="PyMO.fs" />
<Compile Include="Lua.fs" />
<Compile Include="Json.fs" />
<Compile Include="WebGAL.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 11 additions & 3 deletions YukimiScript.CommandLineTool/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let private help () =
" lua Lua 5.1 for Lua Runtime 5.1 or LuaJIT (UTF-8)"
" pymo PyMO 1.2 script, you must compile with libpymo.ykm."
" json Json."
" webgal WebGAL Script (WIP)."
""
"Example:"
" ykmc ./Example/main.ykm --target-pymo ./main.lua -L../lib -lpymo"
Expand Down Expand Up @@ -81,6 +82,7 @@ type private TargetOption =
| PyMO of outputFile: string * scriptName: string
| Bytecode of outputFile: string
| Json of outputFile: string
| WebGAL of outputFile: string


type private DiagramType =
Expand Down Expand Up @@ -129,6 +131,9 @@ let rec private parseTargetsAndOptions (inputSrc: string) =
| "--target-json" :: json :: next ->
parseTargetsAndOptions inputSrc next
|> Result.map (fun (nextTargets, options) -> Json json :: nextTargets, options)
| "--target-webgal" :: webgal :: next ->
parseTargetsAndOptions inputSrc next
|> Result.map (fun (nextTargets, options) -> WebGAL webgal :: nextTargets, options)
| options ->
parseOptions defaultOptions options
|> Result.map (fun options -> [], options)
Expand Down Expand Up @@ -172,7 +177,7 @@ let private doAction errStringing =
function
| Compile (inputFile, targets, options) ->
let libs = loadLibs options
let intermediate = CompilePipe.compile libs inputFile |> unwrapResultExn ErrorStringing.schinese
let dom, intermediate = CompilePipe.compile libs inputFile |> unwrapResultExn ErrorStringing.schinese

targets
|> List.iter
Expand All @@ -193,7 +198,10 @@ let private doAction errStringing =

File.WriteAllText(output, lua, Text.Encoding.UTF8)
| Json out ->
YukimiScript.CodeGen.Json.genJson options.Debugging intermediate out)
YukimiScript.CodeGen.Json.genJson options.Debugging intermediate out
| WebGAL outPath ->
let out = YukimiScript.CodeGen.WebGAL.generateWebGAL dom intermediate
File.WriteAllText(outPath, out.ToString()))

| Diagram (diagramType, inputDir, out, options) ->
let diagramExporter =
Expand Down Expand Up @@ -229,7 +237,7 @@ let private doAction errStringing =
|> Array.toList
|> Result.transposeList
|> unwrapResultExn ErrorStringing.schinese
|> Seq.collect (fun (Intermediate s) -> s)
|> Seq.collect (fun (_, Intermediate s) -> s)
|> Seq.collect (fun x -> x.Block)
|> Seq.collect (fun x -> x.Arguments)
|> Seq.choose (function
Expand Down
4 changes: 2 additions & 2 deletions YukimiScript.Parser/CompilePipe.fs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ let generateIntermediate dom =
|> Result.bind (Dom.expandTextCommands >> Dom.expandUserMacros)
|> Result.bind (fun externAndSysMacro ->
Dom.expandSystemMacros externAndSysMacro
|> Dom.linkToExternCommands
|> Result.map Intermediate.ofDom)
|> Dom.linkToExternCommands)
|> Result.map (fun dom -> dom, Intermediate.ofDom dom)


let compile lib srcPath =
Expand Down