Skip to content

Commit

Permalink
Implement parser optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
CuppoJava committed Oct 10, 2022
2 parents 5eb2731 + 7504833 commit 368579a
Show file tree
Hide file tree
Showing 16 changed files with 581 additions and 260 deletions.
2 changes: 1 addition & 1 deletion compiler/params.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public defn compiler-flags () :
to-tuple(COMPILE-FLAGS)

;========= Stanza Configuration ========
public val STANZA-VERSION = [0 17 24]
public val STANZA-VERSION = [0 17 25]
public var STANZA-INSTALL-DIR:String = ""
public var OUTPUT-PLATFORM:Symbol = `platform
public var STANZA-PKG-DIRS:List<String> = List()
Expand Down
8 changes: 4 additions & 4 deletions core/collections.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public defn HashTable<K,V> (cap0:Int

defn init (c:Int) :
cap = c
limit = c * 5
limit = c * 3 / 4
mask = cap - 1
slots = Array<Sentinel|TableItem<K,V>|Array<TableItem<K,V>>>(cap, sentinel())
sizes = Array<Int>(cap, 0)
Expand Down Expand Up @@ -846,7 +846,7 @@ public defn IntTable<V> (cap0:Int

defn init (c:Int) :
cap = c
limit = c * 5
limit = c * 3 / 4
mask = cap - 1
slots = Array<Sentinel|IntItem<V>|Array<IntItem<V>>>(cap, sentinel())
sizes = Array<Int>(cap, 0)
Expand Down Expand Up @@ -1265,7 +1265,7 @@ public defn HashSet<K> (cap0:Int

defn init (c:Int) :
cap = c
limit = c * 5
limit = c * 3 / 4
mask = cap - 1
slots = Array<Sentinel|SetItem<K>|Array<SetItem<K>>>(cap, sentinel())
sizes = Array<Int>(cap, 0)
Expand Down Expand Up @@ -1519,7 +1519,7 @@ public defn IntSet (cap0:Int) :

defn init (c:Int) :
cap = c
limit = c * 5
limit = c * 3 / 4
mask = cap - 1
slots = Array<Sentinel|Int|Array<Int>>(cap, sentinel())
sizes = Array<Int>(cap, 0)
Expand Down
16 changes: 15 additions & 1 deletion core/parser/binding-analysis.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public defn get-bindings (pat:Pattern) -> List<Binding> :
to-list $ for (b in get-unindexed-bindings(pat), i in 0 to false) seq :
Binding(name(b), i, type(b))

;Return the binding arity of the given pattern.
public defn get-num-binders (p:Pattern) -> Int :
var total:Int = 0
let loop (p:Pattern = p) :
match(p) :
(p:Choice) :
loop(a(p))
(p:Binder) :
total = total + 1
loop(pattern(p))
(p) :
do(loop, p)
total

;<doc>=======================================================
;=================== Compute Binders ========================
;============================================================
Expand Down Expand Up @@ -141,7 +155,7 @@ defn bindable? (p:Pattern) -> True|False :
(p:Empty) : false
(p:NotPat) : false
(p:Binder) : false
(p) : fatal("Unexpected pattern")
(p) : fatal("Unexpected pattern: %_" % [p])

;Returns true if the given pattern contains binders.
defn contains-binders? (p:Pattern) -> True|False :
Expand Down
12 changes: 9 additions & 3 deletions core/parser/engine-frames.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ with:
;iteration.
;- start: The address of the first instruction in loop.
;- end: The address of the first instruction after loop.
;- bind?: True if the loop produces a ListMarker/ListEndMarker pair
; in the value-stack. False if the loop does not.
;- bind-arity: The number of binders captured by the loop pattern.
public defstruct LoopFrame <: StackFrame :
info:FileInfo|False
form:List
upform:List<List>
start:Int
end:Int
num-values:Int
bind?:True|False
bind-arity:Int
with:
printer => true

Expand All @@ -80,7 +79,14 @@ with:

;Used to represent the start of collecting binders for
;executing and action.
;- info: The file information at the time the action was
; started.
;- num-values: The number of values on the value stack at
; the time the action was started.
;- encountered-failure?: During the evaluation of one of
; the binders for this action, Fail was called.
public defstruct ActionFrame <: StackFrame :
info:FileInfo|False
num-values:Int
encountered-failure?:True|False with: (updater => sub-encountered-failure?)
with:
Expand Down
21 changes: 18 additions & 3 deletions core/parser/engine-lang.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,23 @@ pat is an easy case.

public defstruct PatternPackage :
productions:Tuple<DefProduction>
actions:Tuple<(Context -> ?)>
actions:Tuple<ActionCallback>

;Represents a callback for an action.
;- lazy?: True if the binders are lazy and need to be
; explicitly evaluated by the user.
;- bind-arity: The number of binders required by the callback.
public defstruct ActionCallback :
callback: ? -> ?
params: ActionParams

;Represents the parmameters of an action.
;- lazy?: True if the binders are lazy and need to be
; explicitly evaluated by the user.
;- bind-arity: The number of binders required by the callback.
public defstruct ActionParams :
lazy?: True|False
bind-arity:Int

;Represents the definition of a production.
;- public?: True if the production can be referenced by
Expand Down Expand Up @@ -164,7 +180,6 @@ with:
public defstruct Action <: Pattern :
index:Int
guard-index:Int|False
lazy?:True|False
fail?:True|False
pattern:Pattern
with:
Expand Down Expand Up @@ -250,7 +265,7 @@ public defn map (f:Pattern -> Pattern, p:Pattern) -> Pattern :
(p:ListPattern) : ListPattern(f(pattern(p)))
(p:Production) : p
(p:Repeat) : Repeat(f(pattern(p)))
(p:Action) : Action(index(p), guard-index(p), lazy?(p), fail?(p), f(pattern(p)))
(p:Action) : Action(index(p), guard-index(p), fail?(p), f(pattern(p)))
(p:NotPattern) : NotPattern(f(pattern(p)))
(p:Lookahead) : Lookahead(entries(p), else-options(p), g(branches(p)))
(p:SingleBinderGuard) : SingleBinderGuard(index(p), f(pattern(p)))
Expand Down
Loading

0 comments on commit 368579a

Please sign in to comment.