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