-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.os
164 lines (130 loc) · 4.78 KB
/
internal.os
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
!
! BRACY/INTERNAL. Internal representation of a Bracy source.
!
! Copyright © 2014 James B. Moen.
!
! This program is free software: you can redistribute it and/or modify it
! under the terms of the GNU General Public License as published by the Free
! Software Foundation, either version 3 of the License, or (at your option)
! any later version.
!
! This program is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
! FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
! more details.
!
! You should have received a copy of the GNU General Public License along with
! this program. If not, see <http://www.gnu.org/licenses/>.
!
(prog
! Constants that identify internal data objects.
makeTag :− enum()
atomTag :− makeTag() ! Object is an ATOM.
pairTag :− makeTag() ! Object is a PAIR.
markTag :− makeTag() ! Object is a previously visited PAIR.
wordTag :− makeTag() ! Object is a WORD.
! ATOM. An object representing a delimiter or symbol, other than a word. LABEL
! identifies what kind of ATOM this is. NAME may be used to write the ATOM for
! debugging.
atom :−
(tuple
int tag,
int label,
string name)
! PAIR. An object that links other objects together. CAR and CDR both point to
! other objects. Most objects will be PAIRs, so OBJECT is a synonym for PAIR.
pair :−
(tuple
var int tag,
var ref object car,
var ref object cdr)
object :− pair
! WORD. An object representing a word. CHARS are the chars in the word. STYLES
! are the chars's positionally corresponding style sets.
word :−
(tuple
int tag,
string chars,
row set styles)
! IS ATOM. Test if ITEM is an atom.
isAtom :−
(form (ref object item) bool:
(with item :− (past item)
do item ≠ nil ∧ item↑.tag = atomTag))
! IS PAIR. Test if ITEM is a pair. IS SCOPE is a synonym.
isPair :−
(form (ref object item) bool:
(with item :− (past item)
do item ≠ nil ∧ item↑.tag = pairTag))
isScope :− isPair
! IS WORD. Test if ITEM is a word.
isWord :−
(form (ref object item) bool:
(with item :− (past item)
do item ≠ nil ∧ item↑.tag = wordTag))
! MAKE ATOM. Return a pointer to a new ATOM that contains NAME and LABEL.
makeAtom :−
(proc (string name, int label) ref object:
(with item :− fromHeap(var atom)
do item↑.tag := atomTag
item↑.name := name
item↑.label := label
item{ref object}))
! MAKE PAIR. Return a pointer to a new PAIR that holds CAR and CDR.
makePair :−
(proc (ref object car, ref object cdr) ref object:
(with item :− fromHeap(var pair)
do item↑.tag := pairTag
item↑.car := car
item↑.cdr := cdr
item{ref object}))
! MAKE WORD. Return a pointer to a new WORD with copies of CHARS and STYLES.
makeWord :−
(proc (var char0Buffer chars, var setBuffer styles) ref object:
(with
var row var char0 newChars :− fromHeap(length(chars) + 1, var char0)
var row var set newStyles :− fromHeap(length(styles), var set)
ref var word newWord :− fromHeap(var word)
do newWord↑.tag := wordTag
newWord↑.chars := newChars{string}
newWord↑.styles := newStyles{row set}
restart(chars)
(for char0 ch in elements(chars)
do newChars↑ := ch
newChars += 1)
newChars↑ := '\0'
restart(styles)
(for set style in elements(styles)
do newStyles↑ := style
newStyles += 1)
newWord{ref object}))
! UNMAKE. Recursively traverse ITEM, returning it to the heap. ITEM is assumed
! to have no circular pointers.
unmake :−
(proc (ref object item) void:
(with
! UNMAKE PAIR. Return the pair ITEM back to the heap.
unmakePair :−
(form () void:
(with var ref object items :− item
do (while items ≠ nil
do unmake(items↑.car)
items := (items↑.cdr also toHeap(items)))))
! UNMAKE WORD. Return the word ITEM back to the heap.
unmakeWord :−
(form () void:
toHeap(item{ref word}↑.chars)
toHeap(item{ref word}↑.styles)
toHeap(item))
! UNMAKE UNKNOWN. We get here if ITEM has an unknown TAG slot.
unmakeUnknown :−
(form () void:
fail(''Cannot unmake tag %i at %08X.'': item↑.tag, item))
! Dispatch on ITEM's TAG slot. We never return ATOMs to the heap.
do (if item ≠ nil
then (case item↑.tag
of atomTag: skip
pairTag: unmakePair()
wordTag: unmakeWord()
none: unmakeUnknown()))))
)