-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Syuparn/add-examples
Add examples
- Loading branch information
Showing
5 changed files
with
208 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,168 @@ | ||
{{- define "_bf" -}} | ||
{{- if ge .tokPos (len .src) -}} | ||
{{- /* parse ends. do nothing more */ -}} | ||
{{- else -}} | ||
{{- template "_bfParse" $ -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfParse" -}} | ||
{{- $tok := index .src .tokPos -}} | ||
{{- if eq $tok '+' -}} | ||
{{- template "_bfPlus" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok '-' -}} | ||
{{- template "_bfMinus" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok '>' -}} | ||
{{- template "_bfInc" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok '<' -}} | ||
{{- template "_bfDec" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok ',' -}} | ||
{{- template "_bfGet" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok '.' -}} | ||
{{- template "_bfPut" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok '[' -}} | ||
{{- template "_bfWhile" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else if eq $tok ']' -}} | ||
{{- template "_bfEnd" $ -}} | ||
{{- template "_bf" $ -}} | ||
{{- else -}} | ||
{{- /* do nothing and parse next token */ -}} | ||
{{- $_ := set $ "tokPos" (add1 .tokPos) -}} | ||
{{- template "_bf" $ -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfPlus" -}} | ||
{{- $_ := set .mem (toString .ptr) (mod (add1 (get .mem (toString .ptr))) 256) -}} | ||
{{- $_ = set $ "tokPos" (add1 .tokPos) -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfMinus" -}} | ||
{{- /* NOTE: mod cannot be used because mod of minus value is minus! */ -}} | ||
{{- $v := sub (get .mem (toString .ptr)) 1 -}} | ||
{{- $_ := set .mem (toString .ptr) ((ge $v 0) | ternary $v (add $v 256)) -}} | ||
{{- $_ = set $ "tokPos" (add1 .tokPos) -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfInc" -}} | ||
{{- $_ := set $ "ptr" (add1 .ptr) -}} | ||
{{- $_ = set $ "tokPos" (add1 .tokPos) -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfDec" -}} | ||
{{- $_ := set $ "ptr" (sub .ptr 1) -}} | ||
{{- $_ = set $ "tokPos" (add1 .tokPos) -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfGet" -}} | ||
{{- $got := (ge (len .in) 0) | ternary (index .in 0) "" -}} | ||
{{- $_ := set .mem (toString .ptr) $got -}} | ||
{{- $_ = set $ "in" (.in | substr 1 (len .in)) -}} | ||
{{- $_ = set $ "tokPos" (add1 .tokPos) -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfPut" -}} | ||
{{- printf "%c" (index .mem (toString .ptr)) -}} | ||
{{- $_ := set $ "tokPos" (add1 .tokPos) -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfWhile" -}} | ||
{{- if (index .mem (toString .ptr)) -}} | ||
{{- $_ := set $ "tokPos" (add1 .tokPos) -}} | ||
{{- else -}} | ||
{{- /* preceed position to corresponding "]" */ -}} | ||
|
||
{{- $nestCnt := 1 -}} | ||
{{- /* token position to jump */ -}} | ||
{{- $pos := 0 -}} | ||
{{- range $i := untilStep (add1 .tokPos | int) (len .src) 1 -}} | ||
{{- if eq (index $.src $i) '[' -}} | ||
{{- $nestCnt = add1 $nestCnt -}} | ||
{{- else if eq (index $.src $i) ']' -}} | ||
{{- $nestCnt = sub $nestCnt 1 -}} | ||
{{- end -}} | ||
|
||
{{- /* set position just after first corresponding "]" */ -}} | ||
{{- if and (not $pos) (eq $nestCnt 0) -}} | ||
{{- $pos = add1 $i -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- $_ := set $ "tokPos" (default $pos (len .src)) -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- define "_bfEnd" -}} | ||
{{- if not (index .mem (toString .ptr)) -}} | ||
{{- $_ := set $ "tokPos" (add1 .tokPos) -}} | ||
{{- else -}} | ||
{{- /* get position back to corresponding "[" */ -}} | ||
|
||
{{- $nestCnt := 1 -}} | ||
{{- /* token position to jump */ -}} | ||
{{- $pos := -1 -}} | ||
{{- range $i := untilStep (sub .tokPos 1 | int) -1 -1 -}} | ||
{{- if eq (index $.src $i) ']' -}} | ||
{{- $nestCnt = add1 $nestCnt -}} | ||
{{- else if eq (index $.src $i) '[' -}} | ||
{{- $nestCnt = sub $nestCnt 1 -}} | ||
{{- end -}} | ||
|
||
{{- /* set position just after the last corresponding "[" */ -}} | ||
{{- if and (eq $pos -1) (eq $nestCnt 0) -}} | ||
{{- $pos = add1 $i -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- $_ := set $ "tokPos" $pos -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- /* only for debugging */ -}} | ||
{{- define "_bfDump" -}} | ||
{{- println "in: " .in -}} | ||
{{- println "src: " .src -}} | ||
{{- println "ptr: " .ptr -}} | ||
{{- println "tokPos: " .tokPos -}} | ||
{{- range $i := until (len .mem) -}} | ||
{{- printf "%3d " (index $.mem (toString $i)) -}} | ||
{{- if eq (mod $i 16) 15 -}} | ||
{{- println -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- define "bf" -}} | ||
{{- $src := index $ 0 -}} | ||
{{- $in := index $ 1 -}} | ||
|
||
{{- /* initialize memory */ -}} | ||
{{- $MemSize := 1024 -}} | ||
{{- $mem := dict -}} | ||
{{- range $i := until $MemSize -}} | ||
{{- /* HACK: assign to dummy var to be evaluated as "" */ -}} | ||
{{- /* HACK: use dict instead of list to access address in O(1) */ -}} | ||
{{- $_ := set $mem (toString $i) 0 -}} | ||
{{- end -}} | ||
|
||
{{- /* initialize pointer address */ -}} | ||
{{- $ptr := 0 -}} | ||
|
||
{{- /* initialize parsing position */ -}} | ||
{{- $tokPos := 0 -}} | ||
|
||
{{- template "_bf" dict "src" $src "in" $in "mem" $mem "ptr" $ptr "tokPos" $tokPos -}} | ||
{{- end -}} | ||
|
||
{{- /* give source code and stdin */ -}} | ||
{{- template "bf" list ` | ||
+++++++++[>++++++++>+++++++++++>+++>+<<<<-]>.>++.+++++++..+++. | ||
>+++++.<<+++++++++++++++.>.+++.------.--------.>+.>+. | ||
` "" -}} |
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
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,25 @@ | ||
{{- /* Janken (Rock-Paper-Scissors) game */ -}} | ||
{{- /* input your hand to stdin! */ -}} | ||
|
||
{{- /* gu, rock: ✊ */ -}} | ||
{{- /* choki, scissors: ✌ */ -}} | ||
{{- /* pa, paper: 🖐 */ -}} | ||
|
||
{{- /* constants */ -}} | ||
{{- $HandDict := dict "gu" 0 "choki" 1 "pa" 2 "rock" 0 "scissors" 1 "paper" 2 -}} | ||
{{- $HandIcons := list "✊" "✌" "🖐" -}} | ||
|
||
{{- if not (keys $HandDict | has .) -}} | ||
{{- fail (printf "%s cannot be used as a hand!" .) -}} | ||
{{- end -}} | ||
|
||
{{- $playerHand := index $HandDict .}} | ||
{{- $comHand := randInt 0 3 -}} | ||
|
||
{{- /* show result */ -}} | ||
{{- println "You\t\tChallenger" -}} | ||
{{- printf "%s\tVS\t%s\n" (index $HandIcons $playerHand) (index $HandIcons $comHand)}} | ||
|
||
{{- $result := mod (add (sub $playerHand $comHand) 3) 3 -}} | ||
{{- $resultStrs := list "Draw." "You lost..." "You won!" -}} | ||
{{- printf "\n%s\n" (index $resultStrs $result) -}} |
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,3 @@ | ||
{{- $code := `{{- $code := @ -}} | ||
{{- $code | replace (printf "%c" 64) (print "\u0060" $code "\u0060") | println -}}` -}} | ||
{{- $code | replace (printf "%c" 64) (print "\u0060" $code "\u0060") | println -}} |
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,5 @@ | ||
{{- $n := 1 -}} | ||
{{- $code := `{{- $n := ${n} -}} | ||
{{- $code := @ -}} | ||
{{- $code | replace (print "${" "n" "}") (add1 $n | toString) | replace (printf "%c" 64) (print "\u0060" $code "\u0060") | println -}}` -}} | ||
{{- $code | replace (print "${" "n" "}") (add1 $n | toString) | replace (printf "%c" 64) (print "\u0060" $code "\u0060") | println -}} |