begin parser to load from file
This commit is contained in:
@@ -3,12 +3,12 @@ struct
|
||||
open AppType
|
||||
|
||||
fun updateSquares (squares, newX, newY, item) =
|
||||
Vector.mapi
|
||||
(fn (idx, el) =>
|
||||
if idx = newX then
|
||||
Vector.mapi (fn (iidx, iel) => if iidx = newY then item else iel) el
|
||||
else
|
||||
el) squares
|
||||
let
|
||||
val yAxis = Vector.sub (squares, newX)
|
||||
val yAxis = Vector.update (yAxis, newY, item)
|
||||
in
|
||||
Vector.update (squares, newX, yAxis)
|
||||
end
|
||||
|
||||
fun changeSquaresSize (squares, newCanvasWidth, newCanvasHeight) =
|
||||
let
|
||||
|
||||
33
fcore/parsing/brace-dfa.sml
Normal file
33
fcore/parsing/brace-dfa.sml
Normal file
@@ -0,0 +1,33 @@
|
||||
structure BraceDfa =
|
||||
struct
|
||||
val dead = 0
|
||||
val start = 1
|
||||
val final = 2
|
||||
|
||||
fun makeStart i =
|
||||
let
|
||||
val chr = Char.chr i
|
||||
in
|
||||
if chr = #"{" orelse chr = #"}" then
|
||||
final
|
||||
else
|
||||
dead
|
||||
end
|
||||
|
||||
val deadTable = SpaceDfa.deadTable
|
||||
val startTable = Vector.tabulate (255, makeStart)
|
||||
val finalTable = deadTable
|
||||
|
||||
val tables = #[deadTable, startTable, finalTable]
|
||||
|
||||
fun isFinal state =
|
||||
state = final
|
||||
|
||||
fun next (state, chr) =
|
||||
let
|
||||
val table = Vector.sub (tables, state)
|
||||
val idx = Char.ord chr
|
||||
in
|
||||
Vector.sub (table, idx)
|
||||
end
|
||||
end
|
||||
29
fcore/parsing/int-dfa.sml
Normal file
29
fcore/parsing/int-dfa.sml
Normal file
@@ -0,0 +1,29 @@
|
||||
structure IntDfa =
|
||||
MakeDfa (struct
|
||||
val dead = 0
|
||||
val start = 1
|
||||
val final = 2
|
||||
|
||||
fun makeStart i =
|
||||
let
|
||||
val chr = Char.chr i
|
||||
in
|
||||
if i >= #"0" orelse i < #"9" then
|
||||
final
|
||||
else dead
|
||||
end
|
||||
|
||||
val deadTable = SpaceDfa.deadTable
|
||||
val startTable = Vector.tabulate (255, makeStart)
|
||||
val finalTable = startTable
|
||||
|
||||
val tables = #[]deadTable, startTable, finalTable
|
||||
|
||||
fun next (state, chr) =
|
||||
let
|
||||
val table = Vector.sub (tables, state)
|
||||
val idx = Char.ord chr
|
||||
in
|
||||
Vector.sub (table, idx)
|
||||
end
|
||||
end)
|
||||
4
fcore/parsing/lexer.sml
Normal file
4
fcore/parsing/lexer.sml
Normal file
@@ -0,0 +1,4 @@
|
||||
structure Lexer =
|
||||
struct
|
||||
|
||||
end
|
||||
40
fcore/parsing/parse-grid.sml
Normal file
40
fcore/parsing/parse-grid.sml
Normal file
@@ -0,0 +1,40 @@
|
||||
structure ParseGrid =
|
||||
struct
|
||||
fun makeGrid (canvasWidth, canvasHeight) =
|
||||
let
|
||||
val maxPoints = Int.max (canvasWidth, canvasHeight)
|
||||
val emptyYAxis = Vector.tabulate (maxPoints, fn _ => {r = 0, g = 0, b = 0, a = 0})
|
||||
in
|
||||
Vector.tabulate (maxPoints, fn _ => emptyYAxis)
|
||||
end
|
||||
|
||||
local
|
||||
fun loopY (yAxis, x, ex, y, ey, colour) =
|
||||
if y > ey then yAxis
|
||||
else
|
||||
let
|
||||
val yAxis = Vector.update (yAxis, y, colour)
|
||||
in
|
||||
loopY (yAxis, x, ex, y + 1, ey, colour)
|
||||
end
|
||||
|
||||
fun loopX (grid, x, ex, y, ey, colour) =
|
||||
if x > ex then grid
|
||||
else
|
||||
let
|
||||
val yAxis = Vector.sub (grid, x)
|
||||
val yAxis = loopY (yAxis, x, ex, y, ey, colour)
|
||||
val grid = Vector.update (grid, x, yAxis)
|
||||
in
|
||||
loopX (grid, x + 1, ex, y, ey, colour)
|
||||
end
|
||||
in
|
||||
fun applyItem (grid, item) =
|
||||
let
|
||||
val {x, y, ex, ey, r, g, b, a} = item
|
||||
val colour = {r = r, g = g, b = b, a = a}
|
||||
in
|
||||
loopX (grid, x, ex, y, ey, colour)
|
||||
end
|
||||
end
|
||||
end
|
||||
17
fcore/parsing/parsing.md
Normal file
17
fcore/parsing/parsing.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Parsing
|
||||
|
||||
The parsing functionality is for saving and loading from a custom file format.
|
||||
|
||||
The BNF for the custom file format is below.
|
||||
|
||||
Terminals are surrounded by `**` to the left and right. (rule)+ means "1 o more".
|
||||
|
||||
```
|
||||
int ::= (0-9)+
|
||||
|
||||
item ::= **{** int int int int int int int int **}**
|
||||
|
||||
grid ::= int int **{** (item)+ **}**
|
||||
```
|
||||
|
||||
The large number of `int`s in the `item` always follows the order: `x y ex ey r g b a`.
|
||||
34
fcore/parsing/space-dfa.sml
Normal file
34
fcore/parsing/space-dfa.sml
Normal file
@@ -0,0 +1,34 @@
|
||||
structure SpaceDfa =
|
||||
struct
|
||||
val dead = 0
|
||||
val start = 1
|
||||
val final = 2
|
||||
|
||||
fun makeDead _ = 0
|
||||
|
||||
fun makeStart i =
|
||||
let
|
||||
val chr = Char.chr i
|
||||
in
|
||||
if chr = #" " orelse chr = #"\n"
|
||||
then final
|
||||
else dead
|
||||
end
|
||||
|
||||
val deadTable = Vector.tabulate (255, makeDead)
|
||||
val startTable = Vector.tabulate (255, makeStart)
|
||||
val finalTable = startTable
|
||||
|
||||
val tables = #[deadTable, startTable, finalTable]
|
||||
|
||||
fun isFinal state =
|
||||
state = final
|
||||
|
||||
fun next (state, chr) =
|
||||
let
|
||||
val table = Vector.sub (tables, state)
|
||||
val idx = Char.ord chr
|
||||
in
|
||||
Vector.sub (table, idx)
|
||||
end
|
||||
end
|
||||
8
fcore/parsing/tokens.sml
Normal file
8
fcore/parsing/tokens.sml
Normal file
@@ -0,0 +1,8 @@
|
||||
structure Tokens =
|
||||
struct
|
||||
datatype t =
|
||||
L_BRACE
|
||||
| R_BRACE
|
||||
| INT of int
|
||||
| X
|
||||
end
|
||||
Reference in New Issue
Block a user