godprotocol
Version:
A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.
194 lines (175 loc) • 6.69 kB
JavaScript
import Datatypes from "./Datatypes.js";
class Chain extends Datatypes{
constructor(address, account){
super()
this.address = address
this.account = account;
}
chain = async(address)=>{
let chain = new Chain(address, this.account)
return chain;
}
folder = async (path)=>{
let fold = await this.account.ds.folder(this.address)
let folder = path ? await fold.folder(path) : fold
return folder
}
read = async()=>{
let folder = await this.folder()
return await folder.readone()
}
write = async(data)=>{
let folder = await this.folder()
return await folder.write(data)
}
write_config = async(config, options={})=>{
let folder = await this.folder('.configs')
let res = await folder.write(
{...config, _id: options._id || config._id},
{replace: !!config._id || options.replace}
)
return res.datapath
}
add_config = async(config, options)=>{
let result;
switch(config.type){
case 'module':
for (let b=0; b < config.body.length; b++){
let line = config.body[b]
config.body[b] = await this.add_config(line)
}
result = await this.write_config(config, options)
break;
case 'function':
let fn_chain = await this.chain(config.address)
for (let p=0; p < config.parameters.length; p++){
config.parameters[p] = await fn_chain.add_config(config.parameters[p])
}
for (let b=0; b < config.body.length; b++){
config.body[b] = await fn_chain.add_config(config.body[b])
}
result = await fn_chain.write_config(config)
break;
case 'class':
let cls_chain = await this.chain(config.address);
for (let name in config.methods){
let met = config.methods[name]
let res = await cls_chain.add_config(config.methods[name])
config.methods[name] = `${met.address}/${met._id}`
}
for (let name in config.properties){
let met = config.properties[name]
let res = await cls_chain.add_config(config.properties[name])
config.properties[name] = res
}
for (let name in config.class_.properties){
let met = config.class_.properties[name]
let res = await cls_chain.add_config(config.class_.properties[name])
config.class_.properties[name] = res
}
for (let name in config.class_.methods){
let met = config.class_.methods[name]
let res = await cls_chain.add_config(config.class_.methods[name])
config.class_.methods[name] = `${met.address}/${met._id}`
}
result = await cls_chain.write_config(config)
break
case 'condition':
let cond_chain = await this.chain(config.location)
for (let b=0; b< config.blocks.length; b++){
let blk = config.blocks[b]
blk.predicate = await this.add_config(blk.predicate)
for (let d = 0; d <blk.body.length; d++){
blk.body[d] = await this.add_config(blk.body[d])
}
}
result = await cond_chain.write_config(config)
break;
case 'loop':
let loop_chain = await this.chain(config.location)
if (config.loop_type === 'for'){
config.update = await this.add_config(config.update)
config.iterator = await this.add_config(config.iterator)
}
config.condition = await this.add_config(config.condition)
for (let b=0; b< config.body.length; b++){
config.body[b] = await this.add_config(config.body[b])
}
result = await loop_chain.write_config(config)
break;
case 'assignment':
config.value = await this.add_config(config.value)
result = await (await this.chain(`${this.address}/${config.identifier}`)).write_config(config)
break;
case 'nest':
let n_chain = await this.chain(config.location)
for (let n=0; n < config.nests.length; n++){
config.nests[n] = await n_chain.add_config(config.nests[n])
}
result = await n_chain.write_config(config)
break;
case 'return':
config.output = await this.add_config(config.output)
result = config;
break;
case 'call':
let call_chn = await this.chain(config.location)
if (config.identifier.type === 'nest'){
config.identifier = await (await this.chain(config.identifier.location)).write_config(config.identifier)
}
for (let a=0; a < config.arguments.length; a++){
let arg = config.arguments[a]
let chn = await this.chain(arg.location)
config.arguments[a] = await chn.add_config(arg)
}
result = await call_chn.write_config(config)
break
case 'adm':
console.log(config)
let adm_chain = await this.chain(config.location)
result = await adm_chain.write_config(config)
break;
case 'trycatch':
let try_chain = await this.chain(config.location)
for (let i=0; i < config.try_body.length; i++){
let tbd = config.try_body[i]
config.try_body[i] = await try_chain.add_config(tbd)
}
for (let i=0; i < config.catch_body.length; i++){
let tbd = config.catch_body[i]
config.catch_body[i] = await try_chain.add_config(tbd)
}
result = await try_chain.write_config(config)
break;
case 'raise':
let raise_chain = await this.chain(config.location)
config.object = await raise_chain.add_config(config.object)
result = raise_chain.write_config(config)
break
default:
if (config.type === 'void'){
result = config
}else if (this.account.vm.datatypes.includes(config.type)){
if (config.type === 'array'){
for (let i=0; i< config.value.length; i++){
config.value[i] = await this.add_config(config.value[i])
}
} else if (config.type === 'twain'){
for (let key in config.value){
let pair = config.value[key]
pair[0] = await this.add_config(pair[0])
pair[1] = await this.add_config(pair[1])
}
}
let d_chain = await this.chain(config.location)
result = await d_chain.write_config(config)
} else if (['variable', 'reference'].includes(config.type)) {
result = config
} else if (['continue', 'break'].includes(config.type)) {
result = config
}
}
return result;
}
}
export default Chain;