Files
kraken/stdlib/symbol.krak

115 lines
3.6 KiB
Plaintext
Raw Normal View History

2018-05-22 19:43:54 -04:00
import str
import serialize
2018-05-22 19:43:54 -04:00
import vec
import util
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
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
}
fun invalid_symbol(): symbol {
2018-05-22 19:43:54 -04:00
var toRet.construct(str::str("$INVALID$"), false, str::str("$INVALID$")): symbol
return toRet
}
2015-07-15 13:56:57 -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
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
return toRet
}
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
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)
2018-05-22 19:43:54 -04:00
fun symbol(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str, position: int): symbol {
var toRet.construct(nameIn, terminalIn, dataIn): symbol
toRet.position = position
return toRet
}
obj symbol (Object, Serializable) {
2018-05-22 19:43:54 -04:00
var data: str::str
var name: str::str
var terminal: bool
2018-05-22 19:43:54 -04:00
var source: str::str
var position: int
fun construct(): *symbol {
data.construct()
name.construct()
terminal = false
source.construct()
position = 0
return this
}
2018-05-22 19:43:54 -04:00
fun construct(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str): *symbol {
name.construct(nameIn)
terminal = terminalIn
data.construct(dataIn)
source.construct()
position = 0
return this
}
fun destruct() {
data.destruct()
name.destruct()
source.destruct()
}
fun copy_construct(old: *symbol) {
data.copy_construct(&old->data)
name.copy_construct(&old->name)
terminal = old->terminal
source.copy_construct(&old->source)
position = old->position
}
fun operator=(old: ref symbol) {
destruct()
copy_construct(&old)
}
2018-05-22 19:43:54 -04:00
fun serialize(): vec::vec<char> {
return serialize::serialize(data) + serialize::serialize(name) + serialize::serialize(terminal) + serialize::serialize(source) + serialize::serialize(position)
}
2018-05-22 19:43:54 -04:00
fun unserialize(it: ref vec::vec<char>, pos: int): int {
/*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)*/
pos = data.unserialize(it, pos)
pos = name.unserialize(it, pos)
util::unpack(terminal, pos) = serialize::unserialize<bool>(it, pos)
pos = source.unserialize(it, pos)
util::unpack(position, pos) = serialize::unserialize<int>(it, pos)
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;
}
fun operator==(other: ref symbol): bool {
return data == other.data && name == other.name && terminal == other.terminal && source == other.source && position == other.position
}
fun operator!=(other: ref symbol): bool {
return !(*this == other);
}
2018-05-22 19:43:54 -04:00
fun to_string(): str::str {
var terminalString: *char
if (terminal)
terminalString = "true"
else
terminalString = "false"
return name + ": " + data + " " + terminalString + "[" + source + ":" + position + "]"
}
}