slight refactoring (move pure parsing functions to functional core in a separate file)
This commit is contained in:
21
dotscape.mlb
21
dotscape.mlb
@@ -1,28 +1,31 @@
|
|||||||
$(SML_LIB)/basis/basis.mlb
|
$(SML_LIB)/basis/basis.mlb
|
||||||
|
|
||||||
(* FUNCTIONAL CORE *)
|
(* FUNCTIONAL CORE *)
|
||||||
functional-core/app-type.sml
|
functional-core/app/app-type.sml
|
||||||
|
|
||||||
ann
|
ann
|
||||||
"allowVectorExps true"
|
"allowVectorExps true"
|
||||||
in
|
in
|
||||||
functional-core/ndc.sml
|
functional-core/app/ndc.sml
|
||||||
functional-core/click-points.sml
|
functional-core/app/click-points.sml
|
||||||
functional-core/graph-lines.sml
|
functional-core/app/graph-lines.sml
|
||||||
functional-core/triangles.sml
|
functional-core/app/triangles.sml
|
||||||
end
|
end
|
||||||
|
|
||||||
functional-core/triangle-stage.sml
|
functional-core/app/triangle-stage.sml
|
||||||
|
|
||||||
functional-core/app-init.sml
|
functional-core/app/app-init.sml
|
||||||
functional-core/app-with.sml
|
functional-core/app/app-with.sml
|
||||||
|
|
||||||
message-types/input-msg.sml
|
message-types/input-msg.sml
|
||||||
message-types/file-msg.sml
|
message-types/file-msg.sml
|
||||||
message-types/draw-msg.sml
|
message-types/draw-msg.sml
|
||||||
message-types/update-msg.sml
|
message-types/update-msg.sml
|
||||||
|
|
||||||
functional-core/app-update.sml
|
functional-core/app/app-update.sml
|
||||||
|
|
||||||
|
(* pure file parsing functions *)
|
||||||
|
functional-core/file/parse-file.sml
|
||||||
|
|
||||||
(* IMPERATIVE SHELL *)
|
(* IMPERATIVE SHELL *)
|
||||||
$(SML_LIB)/basis/mlton.mlb
|
$(SML_LIB)/basis/mlton.mlb
|
||||||
|
|||||||
55
functional-core/file/parse-file.sml
Normal file
55
functional-core/file/parse-file.sml
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
signature PARSE_FILE =
|
||||||
|
sig
|
||||||
|
val parseLine: string -> AppType.triangle option
|
||||||
|
end
|
||||||
|
|
||||||
|
structure ParseFile :> PARSE_FILE =
|
||||||
|
struct
|
||||||
|
datatype triangle_token = X | Y | COORD of Real32.real | UNKNOWN of string
|
||||||
|
|
||||||
|
fun extractTriangle lst =
|
||||||
|
case lst of
|
||||||
|
[ X, COORD x1
|
||||||
|
, Y, COORD y1
|
||||||
|
, X, COORD x2
|
||||||
|
|
||||||
|
, Y, COORD y2
|
||||||
|
, X, COORD x3
|
||||||
|
, Y, COORD y3
|
||||||
|
] => SOME {x1 = x1, y1 = y1, x2 = x2, y2 = y2, x3 = x3, y3 = y3}
|
||||||
|
| _ => NONE
|
||||||
|
|
||||||
|
fun tokeniseString str =
|
||||||
|
if str = "x" then
|
||||||
|
X
|
||||||
|
else if str = "y" then
|
||||||
|
Y
|
||||||
|
else
|
||||||
|
case Real32.fromString str of
|
||||||
|
SOME num => COORD num
|
||||||
|
| NONE => UNKNOWN str
|
||||||
|
|
||||||
|
fun helpParseLine (line, pos, acc, lastSpacePos) =
|
||||||
|
if pos = String.size line then
|
||||||
|
List.rev acc
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val chr = String.sub (line, pos)
|
||||||
|
in
|
||||||
|
if chr = #" " orelse chr = #"\n" then
|
||||||
|
let
|
||||||
|
val strToken = String.substring
|
||||||
|
(line, lastSpacePos + 1, pos - (lastSpacePos + 1))
|
||||||
|
val token = tokeniseString strToken
|
||||||
|
in
|
||||||
|
helpParseLine (line, pos + 1, token :: acc, pos)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
helpParseLine (line, pos + 1, acc, lastSpacePos)
|
||||||
|
end
|
||||||
|
|
||||||
|
fun parseLine line =
|
||||||
|
let val lst = helpParseLine (line, 0, [], ~1)
|
||||||
|
in extractTriangle lst
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -5,67 +5,18 @@ end
|
|||||||
|
|
||||||
structure FileThread :> FILE_THREAD =
|
structure FileThread :> FILE_THREAD =
|
||||||
struct
|
struct
|
||||||
open AppType
|
|
||||||
open FileMessage
|
open FileMessage
|
||||||
open InputMessage
|
open InputMessage
|
||||||
|
|
||||||
|
datatype parse_result = OK of AppType.triangle list | PARSE_ERROR
|
||||||
|
|
||||||
val filename = "a.dsc"
|
val filename = "a.dsc"
|
||||||
|
|
||||||
datatype triangle_token = X | Y | COORD of Real32.real | UNKNOWN
|
|
||||||
|
|
||||||
fun extractTriangle lst =
|
|
||||||
case lst of
|
|
||||||
[ X, COORD x1
|
|
||||||
, Y, COORD y1
|
|
||||||
, X, COORD x2
|
|
||||||
|
|
||||||
, Y, COORD y2
|
|
||||||
, X, COORD x3
|
|
||||||
, Y, COORD y3
|
|
||||||
] => SOME {x1 = x1, y1 = y1, x2 = x2, y2 = y2, x3 = x3, y3 = y3}
|
|
||||||
| _ => NONE
|
|
||||||
|
|
||||||
fun tokeniseString str =
|
|
||||||
if str = "x" then
|
|
||||||
X
|
|
||||||
else if str = "y" then
|
|
||||||
Y
|
|
||||||
else
|
|
||||||
case Real32.fromString str of
|
|
||||||
SOME num => COORD num
|
|
||||||
| NONE => UNKNOWN
|
|
||||||
|
|
||||||
fun helpParseLine (line, pos, acc, lastSpacePos) =
|
|
||||||
if pos = String.size line then
|
|
||||||
List.rev acc
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val chr = String.sub (line, pos)
|
|
||||||
in
|
|
||||||
if chr = #" " orelse chr = #"\n" then
|
|
||||||
let
|
|
||||||
val strToken = String.substring
|
|
||||||
(line, lastSpacePos + 1, pos - (lastSpacePos + 1))
|
|
||||||
val token = tokeniseString strToken
|
|
||||||
in
|
|
||||||
helpParseLine (line, pos + 1, token :: acc, pos)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
helpParseLine (line, pos + 1, acc, lastSpacePos)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun parseLine line =
|
|
||||||
let val lst = helpParseLine (line, 0, [], ~1)
|
|
||||||
in extractTriangle lst
|
|
||||||
end
|
|
||||||
|
|
||||||
datatype parse_resule = OK of AppType.triangle list | PARSE_ERROR
|
|
||||||
|
|
||||||
fun parse (io, acc) =
|
fun parse (io, acc) =
|
||||||
case TextIO.inputLine io of
|
case TextIO.inputLine io of
|
||||||
SOME line =>
|
SOME line =>
|
||||||
let
|
let
|
||||||
val line = parseLine line
|
val line = ParseFile.parseLine line
|
||||||
in
|
in
|
||||||
(case line of
|
(case line of
|
||||||
SOME tri => parse (io, tri :: acc)
|
SOME tri => parse (io, tri :: acc)
|
||||||
|
|||||||
Reference in New Issue
Block a user