Files
kraken/stdlib/symbol.krak
Nathan Braswell b5ce776726 Basic AST
2018-06-18 19:04:24 -04:00

118 lines
3.7 KiB
Plaintext

import str
import serialize
import vec
import util
fun null_symbol(): symbol {
var toRet.construct(str::str("$NULL$"), false, str::str("$NULL$")): symbol
return toRet
}
fun eof_symbol(): symbol {
var toRet.construct(str::str("$EOF$"), false, str::str("$EOF$")): symbol
return toRet
}
fun invalid_symbol(): symbol {
var toRet.construct(str::str("$INVALID$"), false, str::str("$INVALID$")): symbol
return toRet
}
fun symbol(nameIn: *char, terminalIn: bool): symbol {
var toRet.construct(str::str(nameIn), terminalIn, str::str("no_value")): symbol
return toRet
}
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 {
var toRet.construct(str::str(nameIn), terminalIn, str::str(dataIn)): symbol
return toRet
}
fun symbol(nameIn: ref str::str, terminalIn: bool, dataIn: ref str::str): symbol return symbol(nameIn, terminalIn, dataIn, 0)
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
}
fun to_string(s: ref symbol): str::str {
return s.to_string()
}
obj symbol (Object, Serializable) {
var data: str::str
var name: str::str
var terminal: bool
var source: str::str
var position: int
fun construct(): *symbol {
data.construct()
name.construct()
terminal = false
source.construct()
position = 0
return this
}
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)
}
fun serialize(): vec::vec<char> {
return serialize::serialize(data) + serialize::serialize(name) + serialize::serialize(terminal) + serialize::serialize(source) + serialize::serialize(position)
}
fun unserialize(it: ref vec::vec<char>, pos: int): int {
/*construct()*/
/*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
}
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);
}
fun to_string(): str::str {
var terminalString: *char
if (terminal)
terminalString = "true"
else
terminalString = "false"
return name + ": " + data + " " + terminalString + "[" + source + ":" + position + "]"
}
}