Skip to content
Merged
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
95 changes: 57 additions & 38 deletions src/Core/Qsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,29 @@ module Main =
List.collect Statement.ofFarkdownStatement main

module Action =
let toQspAct (action: Action) =
let toQspAct (optionalSceneId: ParagraphId option) (action: Action) =
Statement.Act(
[Expr.ofFarkdownLine action.Description], [
PosStatement.createSimple (Statement.Proc("gt", [
Expr.Var (VarType.StringType, "curLoc")

match optionalSceneId with
| Some sceneId -> Expr.createSimpleString sceneId
| None -> ()

Expr.createSimpleString action.Reference
]))
]
)

module Paragraph =
let toQsp (paragraph: Paragraph) =
let toQsp optionalSceneId (paragraph: Paragraph) =
let body: Statements =
Main.toQspStatements paragraph.Main
let actions: Statements =
paragraph.Actions
|> List.map (fun action ->
PosStatement.createSimple (Action.toQspAct action)
PosStatement.createSimple (Action.toQspAct optionalSceneId action)
)
[
yield! body
Expand All @@ -124,43 +129,57 @@ module Paragraph =

module Scene =
let toQsp (scene: Scene) =
match scene.Body with
| [singleParagraph] ->
DocumentElement.Location(
Location.Location (
scene.Id,
Paragraph.toQsp singleParagraph
)
)
| paragraphs ->
let body =
List.foldBack
(fun (paragraph: Paragraph) state ->
let expr =
// $args[0] = paragraph.Id
Expr.Expr(
Op.Eq,
Expr.Arr(
(VarType.StringType, "args"),
[Expr.Val (Value.Int 0)]
),
Expr.createSimpleString paragraph.Id
)
[PosStatement.createSimple (Qsp.Ast.If(
expr,
Paragraph.toQsp paragraph,
state
))]
List.foldBack
(fun (paragraph: Paragraph) state ->
let expr =
// $args[1] = paragraph.Id
Expr.Expr(
Op.Eq,
Expr.Arr(
(VarType.StringType, "args"),
[Expr.Val (Value.Int 1)]
),
Expr.createSimpleString paragraph.Id
)
paragraphs
[]
DocumentElement.Location(
Location.Location (scene.Id, body)
[PosStatement.createSimple (Qsp.Ast.If(
expr,
Paragraph.toQsp (Some scene.Id) paragraph,
state
))]
)
scene.Body
[]

module Location =
let toQsp (location: MarkdownCyoa.Core.Location) =
let body =
List.foldBack
(fun (scene: Scene) state ->
let expr =
// $args[0] = scene.Id
Expr.Expr(
Op.Eq,
Expr.Arr(
(VarType.StringType, "args"),
[Expr.Val (Value.Int 0)]
),
Expr.createSimpleString scene.Id
)
[PosStatement.createSimple (Qsp.Ast.If(
expr,
Scene.toQsp scene,
state
))]
)
location.Body
[]

DocumentElement.Location(
Location.Location (
location.Id, body
)
)

let toQsp (markdownCyoaDocument: MarkdownCyoa.Core.Document): Document =
markdownCyoaDocument
|> List.collect (fun location ->
location.Body
|> List.map Scene.toQsp
)
|> List.map Location.toQsp
76 changes: 39 additions & 37 deletions tests/Qsp.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,46 @@ let toMermaidTest =
)
(String.concat "\n" [
String.concat System.Environment.NewLine [
"# chapter-1"
"if $args[0] = 'begin':"
" 'Cinderella stood in the kitchen, her heart heavy with longing. The royal ball was tonight, and she desperately wanted to go. But her stepmother had left her with a mountain of chores. \"If you finish everything by sunset,\" her stepmother had sneered, \"you may go.\"'"
" ''"
" 'Cinderella glanced at the clock. She had only a few hours to clean the entire house, mend her stepsisters'' dresses, and prepare dinner.'"
" ''"
" '---'"
" ''"
" '**What should Cinderella do first?**'"
" ''"
" act 'Start sweeping the floors.': gt $curLoc, 'sweep-floors'"
" act 'Begin mending the dresses.': gt $curLoc, 'mend-dresses'"
" act 'Ask the mice for help.': gt $curLoc, 'ask-mice'"
"elseif $args[0] = 'sweep-floors':"
" 'Cinderella grabbed her broom and began sweeping the dusty floors. As she worked, she hummed a tune to keep her spirits up.'"
" ''"
" 'Suddenly, she noticed a glint under the couch. It was a silver button from one of her stepsisters'' dresses! If she didn''t sew it back on, her stepsister would be furious.'"
" ''"
" '---'"
" ''"
" '**What should Cinderella do now?**'"
" ''"
" act 'Stop sweeping and sew the button back on.': gt $curLoc, 'sew-button'"
" act 'Keep sweeping and deal with the button later.': gt $curLoc, 'keep-sweeping'"
"elseif $args[0] = 'mend-dresses':"
" 'Cinderella sat down with the torn dresses and began stitching. Her fingers moved quickly, but there were so many rips and tears.'"
" ''"
" 'As she worked, she heard the clock chime. Time was running out! She still needed to clean the house and prepare dinner.'"
" ''"
" '---'"
" ''"
" '**What should Cinderella do now?**'"
" ''"
" act 'Finish mending the dresses first.': gt $curLoc, 'finish-mending'"
" act 'Put the dresses aside and start cleaning.': gt $curLoc, 'start-cleaning'"
" act 'Put the dresses aside and start dinner.': gt $curLoc, 'start-cleaning'"
"# location-1"
"if $args[0] = 'chapter-1':"
" if $args[1] = 'begin':"
" 'Cinderella stood in the kitchen, her heart heavy with longing. The royal ball was tonight, and she desperately wanted to go. But her stepmother had left her with a mountain of chores. \"If you finish everything by sunset,\" her stepmother had sneered, \"you may go.\"'"
" ''"
" 'Cinderella glanced at the clock. She had only a few hours to clean the entire house, mend her stepsisters'' dresses, and prepare dinner.'"
" ''"
" '---'"
" ''"
" '**What should Cinderella do first?**'"
" ''"
" act 'Start sweeping the floors.': gt $curLoc, 'chapter-1', 'sweep-floors'"
" act 'Begin mending the dresses.': gt $curLoc, 'chapter-1', 'mend-dresses'"
" act 'Ask the mice for help.': gt $curLoc, 'chapter-1', 'ask-mice'"
" elseif $args[1] = 'sweep-floors':"
" 'Cinderella grabbed her broom and began sweeping the dusty floors. As she worked, she hummed a tune to keep her spirits up.'"
" ''"
" 'Suddenly, she noticed a glint under the couch. It was a silver button from one of her stepsisters'' dresses! If she didn''t sew it back on, her stepsister would be furious.'"
" ''"
" '---'"
" ''"
" '**What should Cinderella do now?**'"
" ''"
" act 'Stop sweeping and sew the button back on.': gt $curLoc, 'chapter-1', 'sew-button'"
" act 'Keep sweeping and deal with the button later.': gt $curLoc, 'chapter-1', 'keep-sweeping'"
" elseif $args[1] = 'mend-dresses':"
" 'Cinderella sat down with the torn dresses and began stitching. Her fingers moved quickly, but there were so many rips and tears.'"
" ''"
" 'As she worked, she heard the clock chime. Time was running out! She still needed to clean the house and prepare dinner.'"
" ''"
" '---'"
" ''"
" '**What should Cinderella do now?**'"
" ''"
" act 'Finish mending the dresses first.': gt $curLoc, 'chapter-1', 'finish-mending'"
" act 'Put the dresses aside and start cleaning.': gt $curLoc, 'chapter-1', 'start-cleaning'"
" act 'Put the dresses aside and start dinner.': gt $curLoc, 'chapter-1', 'start-cleaning'"
" end"
"end"
"--- chapter-1 ----------"
"--- location-1 ----------"
]
])
""
Expand Down
Loading