2018-05-22 19:43:54 -04:00
|
|
|
import str
|
2015-08-26 03:45:34 -04:00
|
|
|
import serialize
|
2018-05-22 19:43:54 -04:00
|
|
|
import vec
|
2015-08-26 03:45:34 -04:00
|
|
|
import util
|
2015-06-28 20:25:27 -04:00
|
|
|
|
2015-07-15 13:56:57 -04:00
|
|
|
fun null_symbol(): symbol {
|
2018-05-22 19:43:54 -04:00
|
|
|
var toRet.construct(str::str("$NULL$"), false, str::str("$NULL$")): symbol
|
2015-08-04 01:07:33 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
fun eof_symbol(): symbol {
|
2018-05-22 19:43:54 -04:00
|
|
|
var toRet.construct(str::str("$EOF$"), false, str::str("$EOF$")): symbol
|
2015-07-15 13:56:57 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
2015-08-06 17:38:41 -04:00
|
|
|
fun invalid_symbol(): symbol {
|
2018-05-22 19:43:54 -04:00
|
|
|
var toRet.construct(str::str("$INVALID$"), false, str::str("$INVALID$")): symbol
|
2015-08-06 17:38:41 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
2015-07-15 13:56:57 -04:00
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun symbol(nameIn: *char, terminalIn: bool): symbol {
|
2018-05-22 19:43:54 -04:00
|
|
|
var toRet.construct(str::str(nameIn), terminalIn, str::str("no_value")): symbol
|
2015-06-28 20:25:27 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
fun symbol(nameIn: ref str::str, terminalIn: bool): symbol {
|
|
|
|
|
var toRet.construct(nameIn, terminalIn, str::str("no_value")): symbol
|
2015-06-29 01:03:51 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun symbol(nameIn: *char, terminalIn: bool, dataIn: *char): symbol {
|
2018-05-22 19:43:54 -04:00
|
|
|
var toRet.construct(str::str(nameIn), terminalIn, str::str(dataIn)): symbol
|
2015-06-29 01:03:51 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
fun symbol(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str): symbol return symbol(nameIn, terminalIn, dataIn, 0)
|
2016-04-05 03:14:56 -04:00
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
fun symbol(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str, position: int): symbol {
|
2015-06-29 01:03:51 -04:00
|
|
|
var toRet.construct(nameIn, terminalIn, dataIn): symbol
|
2016-04-05 03:14:56 -04:00
|
|
|
toRet.position = position
|
2015-06-28 20:25:27 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-18 19:04:24 -04:00
|
|
|
fun to_string(s: ref symbol): str::str {
|
|
|
|
|
return s.to_string()
|
|
|
|
|
}
|
2015-08-26 03:45:34 -04:00
|
|
|
obj symbol (Object, Serializable) {
|
2018-05-22 19:43:54 -04:00
|
|
|
var data: str::str
|
|
|
|
|
var name: str::str
|
2015-06-28 20:25:27 -04:00
|
|
|
var terminal: bool
|
|
|
|
|
|
2018-05-22 19:43:54 -04:00
|
|
|
var source: str::str
|
2016-04-05 03:14:56 -04:00
|
|
|
var position: int
|
|
|
|
|
|
2015-07-04 17:02:51 -04:00
|
|
|
fun construct(): *symbol {
|
2015-06-28 20:25:27 -04:00
|
|
|
data.construct()
|
|
|
|
|
name.construct()
|
2016-04-05 03:14:56 -04:00
|
|
|
terminal = false
|
|
|
|
|
source.construct()
|
|
|
|
|
position = 0
|
2015-06-28 20:25:27 -04:00
|
|
|
return this
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun construct(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str): *symbol {
|
2015-06-28 20:25:27 -04:00
|
|
|
name.construct(nameIn)
|
|
|
|
|
terminal = terminalIn
|
2015-06-29 01:03:51 -04:00
|
|
|
data.construct(dataIn)
|
2016-04-05 03:14:56 -04:00
|
|
|
source.construct()
|
|
|
|
|
position = 0
|
2015-06-28 20:25:27 -04:00
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
fun destruct() {
|
|
|
|
|
data.destruct()
|
|
|
|
|
name.destruct()
|
2016-04-05 03:14:56 -04:00
|
|
|
source.destruct()
|
2015-06-28 20:25:27 -04:00
|
|
|
}
|
2015-07-04 17:02:51 -04:00
|
|
|
fun copy_construct(old: *symbol) {
|
2015-06-28 20:25:27 -04:00
|
|
|
data.copy_construct(&old->data)
|
|
|
|
|
name.copy_construct(&old->name)
|
|
|
|
|
terminal = old->terminal
|
2016-04-05 03:14:56 -04:00
|
|
|
source.copy_construct(&old->source)
|
|
|
|
|
position = old->position
|
2015-06-28 20:25:27 -04:00
|
|
|
}
|
2015-08-04 14:57:56 -04:00
|
|
|
fun operator=(old: ref symbol) {
|
2015-06-28 20:25:27 -04:00
|
|
|
destruct()
|
|
|
|
|
copy_construct(&old)
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun serialize(): vec::vec<char> {
|
2016-04-05 03:14:56 -04:00
|
|
|
return serialize::serialize(data) + serialize::serialize(name) + serialize::serialize(terminal) + serialize::serialize(source) + serialize::serialize(position)
|
2015-08-26 03:45:34 -04:00
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun unserialize(it: ref vec::vec<char>, pos: int): int {
|
2015-08-29 21:45:55 -04:00
|
|
|
/*construct()*/
|
2018-05-22 19:43:54 -04:00
|
|
|
/*util::unpack(data, pos) = serialize::unserialize<str::str>(it, pos)*/
|
|
|
|
|
/*util::unpack(name, pos) = serialize::unserialize<str::str>(it, pos)*/
|
2015-08-29 21:45:55 -04:00
|
|
|
pos = data.unserialize(it, pos)
|
|
|
|
|
pos = name.unserialize(it, pos)
|
2015-08-26 03:45:34 -04:00
|
|
|
util::unpack(terminal, pos) = serialize::unserialize<bool>(it, pos)
|
2016-04-05 03:14:56 -04:00
|
|
|
pos = source.unserialize(it, pos)
|
|
|
|
|
util::unpack(position, pos) = serialize::unserialize<int>(it, pos)
|
2015-08-26 03:45:34 -04:00
|
|
|
return pos
|
|
|
|
|
}
|
2015-12-05 17:31:11 -05:00
|
|
|
fun equal_wo_data(other: ref symbol): bool {
|
|
|
|
|
return name == other.name && terminal == other.terminal;
|
|
|
|
|
}
|
2015-08-04 14:57:56 -04:00
|
|
|
fun operator==(other: ref symbol): bool {
|
2016-04-05 03:14:56 -04:00
|
|
|
return data == other.data && name == other.name && terminal == other.terminal && source == other.source && position == other.position
|
2015-06-28 20:25:27 -04:00
|
|
|
}
|
2015-08-04 14:57:56 -04:00
|
|
|
fun operator!=(other: ref symbol): bool {
|
2015-08-03 18:37:42 -04:00
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun to_string(): str::str {
|
2015-07-04 17:02:51 -04:00
|
|
|
var terminalString: *char
|
2015-06-28 20:25:27 -04:00
|
|
|
if (terminal)
|
|
|
|
|
terminalString = "true"
|
|
|
|
|
else
|
|
|
|
|
terminalString = "false"
|
2016-04-05 03:14:56 -04:00
|
|
|
return name + ": " + data + " " + terminalString + "[" + source + ":" + position + "]"
|
2015-06-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|