Skip to content

Commit 3e093b0

Browse files
committed
Attach TypeInfo
1 parent 2af745f commit 3e093b0

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

YukimiScript.CommandLineTool/Compile.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ let private findRepeat (items: (string * Elements.DebugInformation) seq) =
5252

5353
let checkRepeat errStringing (dom: Dom) =
5454
dom.Externs
55-
|> Seq.map (fun (Elements.ExternCommand (cmd, _), dbg) -> cmd, dbg)
55+
|> Seq.map (fun (Elements.ExternCommand (cmd, _), _, dbg) -> cmd, dbg)
5656
|> findRepeat
5757
|> Seq.tryHead
5858
|> function
@@ -74,7 +74,7 @@ let checkRepeat errStringing (dom: Dom) =
7474
|> unwrapDomException errStringing
7575

7676
dom.Macros
77-
|> Seq.map (fun (s, _, dbg) -> s.Name, dbg)
77+
|> Seq.map (fun (s, _, _, dbg) -> s.Name, dbg)
7878
|> findRepeat
7979
|> Seq.tryHead
8080
|> function

YukimiScript.Parser/Dom.fs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ namespace YukimiScript.Parser
22

33
open YukimiScript.Parser.Parser
44
open YukimiScript.Parser.Elements
5+
open YukimiScript.Parser.TypeChecker
56

67

78
type Dom =
89
{ HangingEmptyLine: DebugInformation list
9-
Externs: (ExternDefination * DebugInformation) list
10-
Macros: (MacroDefination * Block * DebugInformation) list
10+
Externs: (ExternDefination * BlockParamTypes * DebugInformation) list
11+
Macros: (MacroDefination * BlockParamTypes * Block * DebugInformation) list
1112
Scenes: (SceneDefination * Block * DebugInformation) list }
1213

1314

@@ -46,6 +47,9 @@ module Dom =
4647
exception ExternRepeatException of name: string * debugInfo: DebugInformation seq
4748

4849

50+
exception ExternCannotHasContentException of name: string * DebugInformation
51+
52+
4953
let private saveCurrentBlock state =
5054
match state.CurrentBlock with
5155
| None -> state
@@ -56,17 +60,32 @@ module Dom =
5660
Macros =
5761
match label with
5862
| MacroDefination x ->
59-
(x, List.rev block, debugInfo)
60-
:: state.Result.Macros
61-
| SceneDefination _ -> state.Result.Macros
62-
| _ -> raise UnknownException
63+
let block = List.rev block
64+
match parametersTypeFromBlock x.Param block with
65+
| Ok t -> (x, t, block, debugInfo) :: state.Result.Macros
66+
| Error e -> raise e
67+
| _ -> state.Result.Macros
6368
Scenes =
6469
match label with
65-
| MacroDefination _ -> state.Result.Scenes
6670
| SceneDefination x ->
6771
(x, List.rev block, debugInfo)
6872
:: state.Result.Scenes
69-
| _ -> raise UnknownException } }
73+
| _ -> state.Result.Scenes
74+
Externs =
75+
match label with
76+
| ExternDefination (ExternCommand (n, p)) ->
77+
if
78+
List.forall (fst >> function
79+
| CommandCall c when c.Callee = "__type" -> true
80+
| _ -> false) block
81+
then
82+
match parametersTypeFromBlock p block with
83+
| Ok t ->
84+
(ExternCommand (n, p), t, debugInfo)
85+
:: state.Result.Externs
86+
| Error e -> raise e
87+
else raise <| ExternCannotHasContentException (n, debugInfo)
88+
| _ -> state.Result.Externs} }
7089

7190

7291
let private analyzeFold state (line, debugInfo) =
@@ -98,16 +117,7 @@ module Dom =
98117
| Line.Text x -> pushOperation <| Text x
99118
| SceneDefination scene -> Ok <| setLabel state (SceneDefination scene)
100119
| MacroDefination macro -> Ok <| setLabel state (MacroDefination macro)
101-
| ExternDefination (ExternCommand (name, param)) ->
102-
let nextState = saveCurrentBlock state
103-
104-
{ nextState with
105-
Result =
106-
{ nextState.Result with
107-
Externs =
108-
(ExternCommand(name, param), debugInfo)
109-
:: nextState.Result.Externs } }
110-
|> Ok
120+
| ExternDefination extern' -> Ok <| setLabel state (ExternDefination extern')
111121

112122

113123
exception CannotDefineSceneInLibException of string
@@ -138,28 +148,23 @@ module Dom =
138148

139149

140150
let expandTextCommands (x: Dom) : Dom =
141-
let mapBlock (defination, block, debugInfo) =
142-
let block =
143-
block
144-
|> List.collect
145-
(function
146-
| Text x, debugInfo ->
147-
[ if debugInfo.Comment.IsSome then
148-
EmptyLine, debugInfo
149-
150-
yield! Text.expandTextBlock x debugInfo ]
151-
| x -> [ x ])
151+
let mapBlock =
152+
List.collect (function
153+
| Text x, debugInfo ->
154+
[ if debugInfo.Comment.IsSome then
155+
EmptyLine, debugInfo
152156

153-
defination, block, debugInfo
157+
yield! Text.expandTextBlock x debugInfo ]
158+
| x -> [ x ])
154159

155160
{ x with
156-
Scenes = List.map mapBlock x.Scenes
157-
Macros = List.map mapBlock x.Macros }
161+
Scenes = List.map (fun (def, block, d) -> def, mapBlock block, d) x.Scenes
162+
Macros = List.map (fun (def, t, b, d) -> def, t, mapBlock b, d) x.Macros }
158163

159164

160165
let expandUserMacros (x: Dom) =
161166
let macros =
162-
List.map (fun (a, b, _) -> a, b) x.Macros
167+
List.map (fun (a, _, b, _) -> a, b) x.Macros
163168

164169
x.Scenes
165170
|> List.map
@@ -199,7 +204,7 @@ module Dom =
199204

200205

201206
let linkToExternCommands (x: Dom) : Result<Dom, exn> =
202-
let externs = systemCommands @ List.map fst x.Externs
207+
let externs = systemCommands @ List.map (fun (x, _, _) -> x) x.Externs
203208

204209
let linkSingleCommand (op, debugInfo) =
205210
match op with

YukimiScript.Parser/TypeChecker.fs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module Types =
1919
let number = ParameterType ("number", set [ Int'; Real' ])
2020
let real = ParameterType ("real", set [ Real' ])
2121
let symbol = ParameterType ("symbol", set [ Symbol' ])
22-
let void' = ParameterType ("void", set [])
23-
let all = [ any; int; number; real; symbol; void' ]
22+
let all = [ any; int; number; real; symbol ]
2423

2524

2625
let sumParameterType (ParameterType (n1, s1)) (ParameterType (n2, s2)) =
@@ -56,7 +55,10 @@ let matchCall d (pars: ParameterType list) (argType: Constant list) =
5655
exception IsNotAType of string
5756

5857

59-
let parametersTypeFromBlock (par: Parameter list) (b: Block) =
58+
type BlockParamTypes = (string * ParameterType) list
59+
60+
61+
let parametersTypeFromBlock (par: Parameter list) (b: Block) : Result<BlockParamTypes, exn> =
6062
let typeMacroParams =
6163
[ { Parameter = "param"; Default = None }
6264
{ Parameter = "type"; Default = None } ]
@@ -79,7 +81,7 @@ let parametersTypeFromBlock (par: Parameter list) (b: Block) =
7981
|> List.filter (fst >> (=) name)
8082
|> List.map snd
8183
|> function
82-
| [] -> Ok Types.any
84+
| [] -> Ok (name, Types.any)
8385
| types ->
8486
types
8587
|> List.map (fun typeName ->
@@ -89,5 +91,6 @@ let parametersTypeFromBlock (par: Parameter list) (b: Block) =
8991
| Some x -> Ok x
9092
| None -> Error <| IsNotAType typeName)
9193
|> ParserMonad.sequenceRL
92-
|> Result.map (List.reduce sumParameterType))
94+
|> Result.map (fun t ->
95+
name, List.reduce sumParameterType t))
9396
|> ParserMonad.sequenceRL)

0 commit comments

Comments
 (0)