本文整理汇总了C++中SExpr类的典型用法代码示例。如果您正苦于以下问题:C++ SExpr类的具体用法?C++ SExpr怎么用?C++ SExpr使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SExpr类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: compile_error
// Parsing nth argument (output = -1)
pair<string,ProtoType*> ProtoInterpreter::parse_argument(SE_List_iter* i, int n,
Signature* sig, bool anonymous_ok) {
SExpr *a = i->get_next("argument");
SExpr *b = (i->on_token("|")) ? i->get_next("argument") : NULL;
string name=""; ProtoType* type=NULL;
if(b) { // specified as name|type
if(sexp_is_type(a))
compile_error(a,"Parameter name cannot be a type");
if(a->isSymbol()) name = dynamic_cast<SE_Symbol &>(*a).name;
else compile_error(a,"Parameter name not a symbol: "+ce2s(a));
type = sexp_to_type(b);
} else { // determine name or type by parsing
if(sexp_is_type(a)) {
if(anonymous_ok) type = sexp_to_type(a);
else compile_error(a,"Function parameters must be named: "+ce2s(a));
}
else if(a->isSymbol()) name = dynamic_cast<SE_Symbol &>(*a).name;
else compile_error(a,"Parameter name not a symbol: "+ce2s(a));
}
// fall back to defaults where needed
if(name=="") name = (n>=0) ? ("arg"+i2s(n)) : "value";
if(type==NULL) type = new ProtoType();
// record name in signature and return
if(sig->names.count(name))
compile_error(a,"Cannot bind '"+name+"': already bound");
sig->names[name] = n;
return make_pair(name,type);
}
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:29,代码来源:interpreter.cpp
示例2: throw
void SExpr::toStream(std::ostream& out, const SExpr& sexpr, OutputLanguage language, int indent) throw() {
if( sexpr.isKeyword() && languageQuotesKeywords(language) ){
out << quoteSymbol(sexpr.getValue());
} else {
toStreamRec(out, sexpr, language, indent);
}
}
开发者ID:g2graman,项目名称:CVC4,代码行数:7,代码来源:sexpr.cpp
示例3: m_env
SExpr* ProtoInterpreter::expand_macro(MacroOperator* m, SE_List* call) {
Env m_env(this);
// bind variables to SExprs
if(!m->signature->legal_length(call->len()-1))
return sexp_err(call,"Wrong number of arguments for macro "+m->name);
int i=1; // start after macro name
for(int j=0;j<m->signature->required_inputs.size();j++) {
ProtoSymbol* var
= &dynamic_cast<ProtoSymbol &>(*m->signature->required_inputs[j]);
m_env.bind(var->value,(*call)[i++]);
}
for(int j=0;j<m->signature->optional_inputs.size() && i<call->len();j++) {
ProtoSymbol* var
= &dynamic_cast<ProtoSymbol &>(*m->signature->optional_inputs[j]);
m_env.bind(var->value,(*call)[i++]);
}
if(m->signature->rest_input) { // sweep all else into rest argument
ProtoSymbol* var
= &dynamic_cast<ProtoSymbol &>(*m->signature->rest_input);
SE_List *rest = new SE_List(); rest->inherit_attributes(m);
for(; i<call->len(); ) { rest->add((*call)[i++]); }
m_env.bind(var->value,rest);
}
// then substitute the pattern
V3 << "Expand macro call:\n"; V3 << call->to_str() << endl;
SExpr* expanded = macro_substitute(m->pattern,&m_env);
V3 << "Macro expanded into:\n"; V3 << expanded->to_str() << endl;
return expanded;
}
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:29,代码来源:interpreter.cpp
示例4: throw
CVC4::SExpr SmtEngine::getOption(const std::string& key) const
throw(OptionException) {
NodeManagerScope nms(d_nodeManager);
Trace("smt") << "SMT getOption(" << key << ")" << endl;
if(key.length() >= 18 &&
key.compare(0, 18, "command-verbosity:") == 0) {
map<string, Integer>::const_iterator i = d_commandVerbosity.find(key.c_str() + 18);
if(i != d_commandVerbosity.end()) {
return (*i).second;
}
i = d_commandVerbosity.find("*");
if(i != d_commandVerbosity.end()) {
return (*i).second;
}
return Integer(2);
}
if(Dump.isOn("benchmark")) {
Dump("benchmark") << GetOptionCommand(key);
}
if(key == "command-verbosity") {
vector<SExpr> result;
SExpr defaultVerbosity;
for(map<string, Integer>::const_iterator i = d_commandVerbosity.begin();
i != d_commandVerbosity.end();
++i) {
vector<SExpr> v;
v.push_back((*i).first);
v.push_back((*i).second);
if((*i).first == "*") {
// put the default at the end of the SExpr
defaultVerbosity = v;
} else {
result.push_back(v);
}
}
// put the default at the end of the SExpr
if(!defaultVerbosity.isAtom()) {
result.push_back(defaultVerbosity);
} else {
// ensure the default is always listed
vector<SExpr> v;
v.push_back("*");
v.push_back(Integer(2));
result.push_back(v);
}
return result;
}
${smt_getoption_handlers}
#line 134 "${template}"
throw UnrecognizedOptionException(key);
}
开发者ID:jgorzny,项目名称:CVC4,代码行数:59,代码来源:smt_options_template.cpp
示例5: pushState
bool Game::pollSource()
{
if(m_sexprSource == 0)
{
return false;
}
if(m_sexprSource->sexprReady())
{
SExpr sexpr = m_sexprSource->getSExpr();
if(sexpr.type() == NodeType::Unknown)
{
return false;
}
if(sexpr[0] == "status")
{
GameState* state = new GameState;
state->fromSExpr(sexpr);
pushState(state);
return true;
}
//Ident message
//("ident" (("0" "MuesAI" "Stephen Mues" "human") ("1" "MuesAI" "Stephen Mues" "zombie")) "0")
if(sexpr[0] == "ident")
{
SExpr ident = sexpr.next().list();
if(ident.list()[3] == "zombie")
{
m_zombieName = ident.list()[2];
m_humanName = ident.next().list()[2];
}
else
{
m_humanName = ident.list()[2];
m_zombieName = ident.next().list()[2];
}
}
if(sexpr[0] == "notification")
{
if(sexpr[1] == Config::instance()->get("user") || atoi(Config::instance()->get("arenaMode").c_str()) > 0)
{
cout << "A game was joined by the same player that this visualizer is logged in as" << endl;
if( atoi(Config::instance()->get("autoJoin").c_str()) > 0 || atoi(Config::instance()->get("arenaMode").c_str()) > 0)
{
cout << "Stopping reader thread" << endl;
m_nextGame = sexpr[2];
Engine::instance()->stopPolling();
//throw "die";
}
}
}
}
return false;
}
开发者ID:,项目名称:,代码行数:60,代码来源:
示例6: throw
void Printer::toStream(std::ostream& out, const SExpr& sexpr) const throw() {
if(sexpr.isInteger()) {
out << sexpr.getIntegerValue();
} else if(sexpr.isRational()) {
out << fixed << sexpr.getRationalValue().getDouble();
} else if(sexpr.isKeyword()) {
out << sexpr.getValue();
} else if(sexpr.isString()) {
string s = sexpr.getValue();
// escape backslash and quote
for(size_t i = 0; i < s.length(); ++i) {
if(s[i] == '"') {
s.replace(i, 1, "\\\"");
++i;
} else if(s[i] == '\\') {
s.replace(i, 1, "\\\\");
++i;
}
}
out << "\"" << s << "\"";
} else {
out << '(';
const vector<SExpr>& kids = sexpr.getChildren();
bool first = true;
for(vector<SExpr>::const_iterator i = kids.begin(); i != kids.end(); ++i) {
if(first) {
first = false;
} else {
out << ' ';
}
out << *i;
}
out << ')';
}
}/* Printer::toStream(SExpr) */
开发者ID:xcodevn,项目名称:CVC4,代码行数:35,代码来源:printer.cpp
示例7: if
ProtoType* ProtoInterpreter::sexp_to_type(SExpr* s) {
if(s->isSymbol()) {
const string &name = dynamic_cast<SE_Symbol &>(*s).name;
if(name=="any") { return new ProtoType();
} else if(name=="local") { return new ProtoLocal();
} else if(name=="tuple") { return new ProtoTuple();
} else if(name=="symbol") { return new ProtoSymbol();
} else if(name=="number") { return new ProtoNumber();
} else if(name=="scalar") { return new ProtoScalar();
} else if(name=="boolean") { return new ProtoBoolean();
} else if(name=="vector") { return new ProtoVector();
} else if(name=="lambda" || name=="fun") { return new ProtoLambda();
} else if(name=="field") { return new ProtoField();
} else { return type_err(s,"Unknown type "+s->to_str());
}
} else if(s->isList()) {
SE_List* sl = &dynamic_cast<SE_List &>(*s);
if(!sl->op()->isSymbol())
return type_err(s,"Compound type must start with symbol: "+ce2s(s));
const string &name = dynamic_cast<SE_Symbol &>(*sl->op()).name;
if(name=="tuple" || name=="vector") {
ProtoTuple* t;
if(name=="tuple") t=new ProtoTuple(true); else t=new ProtoVector(true);
for(int i=1;i<sl->len();i++) {
SExpr* subex = (*sl)[i];
if(subex->isSymbol()
&& dynamic_cast<SE_Symbol &>(*subex).name=="&rest") {
t->bounded=false; continue;
}
ProtoType* sub = sexp_to_type(subex);
if(name=="vector" && !sub->isA("ProtoScalar"))
return type_err(sl,"Vectors must contain only scalars");
t->types.push_back(sub);
}
return t;
} else if(name=="lambda" || name=="fun") {
if(sl->len()!=3) return type_err(s,"Bad lambda type: "+s->to_str());
Signature* sig = sexp_to_sig((*sl)[1]);
sig->output = sexp_to_type((*sl)[2]);
return new ProtoLambda(new Operator(s,sig));
} else if(name=="field") {
if(sl->len()!=2) return type_err(s,"Bad field type: "+s->to_str());
ProtoType* sub = sexp_to_type((*sl)[1]);
if(sub->isA("ProtoField"))
return type_err(s,"Field type must have a local subtype");
return new ProtoField(sub);
} else {
return type_err(s,"Unknown type "+s->to_str());
}
} else { // scalars specify ProtoScalar literals
return new ProtoScalar(dynamic_cast<SE_Scalar &>(*s).value);
}
}
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:53,代码来源:interpreter.cpp
示例8: parse_primitive_attributes
// Parse the extra arguments of a primitive into attributes
void parse_primitive_attributes(SE_List_iter* li,Primitive* p) {
while(li->has_next()) {
SExpr* v = li->get_next();
if(!v->isKeyword()) {compile_error(v,v->to_str()+" not a keyword"); return;}
const string &name = dynamic_cast<SE_Symbol &>(*v).name;
if(p->attributes.count(name))
compile_warn("Primitive "+p->name+" overriding duplicate '"
+name+"' attribute");
if(li->has_next() && !li->peek_next()->isKeyword()) {
p->attributes[name]=new SExprAttribute(li->get_next());
} else {
p->attributes[name]=new MarkerAttribute(true);
}
}
}
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:16,代码来源:interpreter.cpp
示例9: make_gensym
// walks through, copying (and inheriting attributes)
SExpr* ProtoInterpreter::macro_substitute(SExpr* src, Env* e, SE_List* wrapper) {
if(is_gensym(src)) { // substitute with a gensym for this instance
SE_Symbol *groot = &dynamic_cast<SE_Symbol &>(*src);
SExpr *gensym;
CompilationElement *element = e->lookup(groot->name);
if (element == 0) {
gensym = make_gensym(groot->name);
gensym->inherit_attributes(src);
e->bind(groot->name, gensym);
} else {
gensym = &dynamic_cast<SExpr &>(*element);
}
return gensym;
} else if(src->isList()) { // SE_List
SE_List *srcl = &dynamic_cast<SE_List &>(*src);
string opname
= ((*srcl)[0]->isSymbol() ? dynamic_cast<SE_Symbol &>(*(*srcl)[0]).name
: "");
if(opname=="comma") {
if(srcl->len()!=2 || !(*srcl)[1]->isSymbol())
return sexp_err(src,"Bad comma form: "+src->to_str());
SE_Symbol* sn = &dynamic_cast<SE_Symbol &>(*(*srcl)[1]);
// insert source text
return dynamic_cast<SExpr &>(*e->lookup(sn,"SExpr")).copy();
} else if(opname=="comma-splice") {
if(wrapper==NULL)
return sexp_err(src,"Comma-splice "+(*srcl)[0]->to_str()+" w/o list");
if(srcl->len()!=2 || !(*srcl)[1]->isSymbol())
return sexp_err(src,"Bad comma form: "+src->to_str());
SE_Symbol* sn = &dynamic_cast<SE_Symbol &>(*(*srcl)[1]);
SE_List* value = &dynamic_cast<SE_List &>(*e->lookup(sn,"SE_List"));
for(int i=0;i<value->len();i++)
wrapper->add((*value)[i]->copy());
return NULL; // comma-splices return null
} else { // otherwise, just substitute each child
SE_List *l = new SE_List();
SE_List *srcl = &dynamic_cast<SE_List &>(*src);
for(int i=0;i<srcl->len();i++) {
SExpr* sub = macro_substitute((*srcl)[i],e,l);
if(sub!=NULL) l->add(sub); // comma-splices add selves and return null
}
return l;
}
} else { // symbol or scalar
return src->copy();
}
}
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:48,代码来源:interpreter.cpp
示例10: scope
void SExpr::toStreamRec(std::ostream& out, const SExpr& sexpr,
OutputLanguage language, int indent) {
StreamFormatScope scope(out);
if (sexpr.isInteger()) {
out << sexpr.getIntegerValue();
} else if (sexpr.isRational()) {
const double approximation = sexpr.getRationalValue().getDouble();
out << std::fixed << approximation;
} else if (sexpr.isKeyword()) {
out << sexpr.getValue();
} else if (sexpr.isString()) {
std::string s = sexpr.getValue();
// escape backslash and quote
for (size_t i = 0; i < s.length(); ++i) {
if (s[i] == '"') {
s.replace(i, 1, "\\\"");
++i;
} else if (s[i] == '\\') {
s.replace(i, 1, "\\\\");
++i;
}
}
out << "\"" << s << "\"";
} else {
const std::vector<SExpr>& kids = sexpr.getChildren();
out << (indent > 0 && kids.size() > 1 ? "( " : "(");
bool first = true;
for (std::vector<SExpr>::const_iterator i = kids.begin(); i != kids.end();
++i) {
if (first) {
first = false;
} else {
if (indent > 0) {
out << "\n" << std::string(indent, ' ');
} else {
out << ' ';
}
}
toStreamRec(out, *i, language,
indent <= 0 || indent > 2 ? 0 : indent + 2);
}
if (indent > 0 && kids.size() > 1) {
out << '\n';
if (indent > 2) {
out << std::string(indent - 2, ' ');
}
}
out << ')';
}
} /* toStreamRec() */
开发者ID:CVC4,项目名称:CVC4,代码行数:51,代码来源:sexpr.cpp
示例11: convertInvoke
SExpr convertInvoke(const SExpr& invoke) {
std::string resultString = "call " + currentModule_->exportedFunction(invoke[1].value())->name() + " ";
for (std::size_t i = 2; i < invoke.children().size(); i++) {
resultString += invoke[i].toString();
resultString += " ";
}
SExpr result = SExprParser::parseString(resultString);
return result;
}
开发者ID:WebAssembly,项目名称:wasmint,代码行数:9,代码来源:main.cpp
示例12: ArithRRNode
SExpr* SPrimScope::inlineEQ() {
if (PrintInlining) lprintf("%*s*inlining _Eq\n", (void*)(depth-1), "");
NodeGen* ng = theNodeGen;
if (SICDebug) ng->comment("inlined _Eq");
SExpr* arg = args->top();
PReg* r = receiver->preg();
PReg* ar = arg->preg();
ng->append(new ArithRRNode(SubCCArithOp, r, ar, ng->noPR));
Node* test = ng->append(new BranchNode(EQBranchOp));
Node* falseNode;
test->append(falseNode = new AssignNode(falsePR(), resultPR));
SExpr* e1 = new ConstantSExpr(Memory->falseObj, resultPR, falseNode);
MergeNode* merge = new MergeNode("inlineEQ merge");
falseNode->append(merge);
Node* trueNode = new AssignNode(truePR(), resultPR);
ng->current = test->append1(trueNode);
SExpr* e2 = new ConstantSExpr(Memory->trueObj, resultPR, trueNode);
ng->branch(merge);
SExpr* res = e1->copyMergeWith(e2, resultPR, ng->current);
return res;
}
开发者ID:AaronNGray,项目名称:self,代码行数:21,代码来源:sicPrimline.cpp
示例13: getChildren
bool SExpr::operator==(const SExpr& s) const {
if (d_sexprType == s.d_sexprType && d_integerValue == s.d_integerValue &&
d_rationalValue == s.d_rationalValue &&
d_stringValue == s.d_stringValue) {
if (d_children == NULL && s.d_children == NULL) {
return true;
} else if (d_children != NULL && s.d_children != NULL) {
return getChildren() == s.getChildren();
}
}
return false;
}
开发者ID:CVC4,项目名称:CVC4,代码行数:12,代码来源:sexpr.cpp
示例14: Unused
lookupTarget* sicScopeLookupTarget::get_target_for_slot(slotDesc* s,
simpleLookup* L) {
Unused(L);
if (s->name == VMString[LEXICAL_PARENT]) {
return new sicScopeLookupTarget(scope->parent());
} else if (s->name == VMString[SELF]) {
SExpr* t = scope->receiverExpr();
if (t->isConstantSExpr()) {
return (new objectLookupTarget(t->constant())) -> be_receiver();
} else if (t->hasMap()) {
return ( new mapLookupTarget(t->map())) -> be_receiver();
} else {
// don't know the receiver type
return NULL;
}
} else if (! s->is_map_slot()) {
return NULL;
} else {
return new objectLookupTarget(s->data);
}
}
开发者ID:ardeujho,项目名称:self,代码行数:21,代码来源:target.cpp
示例15: opcode_for_selector
SExpr* SPrimScope::inlineIntArithmetic() {
ArithOpCode op = opcode_for_selector(_selector);
bool intRcvr =
receiver->hasMap() && receiver->map() == Memory->smi_map;
SExpr* arg = args->nth(0);
bool intArg = arg->hasMap() && arg->map() == Memory->smi_map;
if ( intArg
&& arg->isConstantSExpr()
&& intRcvr
&& arg->constant() == as_smiOop(0)
&& can_fold_rcvr_op_zero_to_zero(op)) {
if (PrintInlining)
lprintf("%*s*constant-folding %s: 0\n", (void*)(depth-1), "", ArithOpName[op]);
return receiver;
}
if (PrintInlining) lprintf("%*s*inlining %s:\n", (void*)(depth-1),
"", ArithOpName[op]);
if (!TArithRRNode::isOpInlinable(op))
return NULL;
NodeGen* n = theNodeGen;
Node* arith = n->append(new TArithRRNode(op, receiver->preg(), arg->preg(),
resultPR, intRcvr, intArg));
// success case - no overflow, int tags
MergeNode* ok = (MergeNode*)n->append(new MergeNode("inlineIntArithmetic ok"));
SExpr* succExpr = new MapSExpr(Memory->smi_map->enclosing_mapOop(), resultPR, ok);
// merge of success & failure branches
MergeNode* done = (MergeNode*)ok->append(new MergeNode("inlineIntArithmetic done"));
// failure case
n->current = arith->append1(new NopNode);
if (theSIC->useUncommonTraps &&
sender()->rscope->isUncommonAt(sender()->bci(), true)) {
n->uncommonBranch(currentExprStack(0), true);
n->current = done;
if (PrintInlining) {
lprintf("%*s*making arithmetic failure uncommon\n", (void*)(depth-1),
"");
}
return succExpr;
} else {
fint b = bci();
PReg* error = new SAPReg(_sender, b, b);
if (intRcvr && intArg) {
// must be overflow
n->loadOop(VMString[OVERFLOWERROR], error);
} else {
arith->hasSideEffects_now = true; // may fail, so can't eliminate
if (intRcvr || TARGET_ARCH == I386_ARCH) {
// arg & TagMask already done by TArithRRNode
// I386 does 'em all
} else {
PReg* t = new TempPReg(this, Temp1, false, true);
n->append(new ArithRCNode(AndCCArithOp, t, Tag_Mask, t));
n->current->hasSideEffects_now = true;
}
// Note: this code assumes that condcode EQ means overflow
Node* branch = n->append(new BranchNode(EQBranchOp));
// no overflow, must be type error
n->loadOop(VMString[BADTYPEERROR], error);
MergeNode* cont = (MergeNode*)n->append(
new MergeNode("inlineIntArithmetic cont"));
// overflow error
PReg* err = new_ConstPReg(_sender, VMString[OVERFLOWERROR]);
n->current = branch->append1(new AssignNode(err, error));
n->branch(cont);
}
Node* dummy;
SExpr* failExpr = genPrimFailure(NULL, error, dummy, done, resultPR);
assert(done, "merge should always exist");
return succExpr->mergeWith(failExpr, done);
}
}
开发者ID:AaronNGray,项目名称:self,代码行数:76,代码来源:sicPrimline.cpp
示例16: assert
const char* NodeGen::splitCondBranch( MergeNode* targetNode,
bool isBackwards,
PReg* targetPR,
SExpr* testExpr,
BranchBCTargetStack* targetStack,
SExprStack* exprStack,
PRegBList* exprStackPRs,
SplitSig* s ) {
// try to split a conditional branch bc to avoid materializing
// the boolean
// local splitting only for now
assert(targetPR->isConstPReg(),
"cond branch must be testing for constant");
oop targetOop = ((ConstPReg*)targetPR)->constant;
if (!testExpr->isMergeSExpr())
return "testExpr not MergeSExpr";
if (!((MergeSExpr*)testExpr)->isSplittable())
return "textExpr not splittable";
SExprBList* exprs = ((MergeSExpr*)testExpr)->exprs;
assert(testExpr->node(), "splittable implies node()");
Node* preceedingMerge = testExpr->node();
if (current != preceedingMerge)
return "not local"; // local only for now
if ( preceedingMerge->nPredecessors() != exprs->length() )
return "would have to iterate over predecessors";
fint i;
for ( i = 0;
i < exprs->length();
++i) {
SExpr* e = exprs->nth(i);
Node* n = e->node();
if ( !preceedingMerge->isPredecessor(n) )
return "merge not immediately after expr node";
if ( !e->isConstantSExpr() )
return "merge contains non-constant expression";
}
MergeNode* mergeForBranching = new MergeNode("for branching");
MergeNode* mergeForFallingThrough = new MergeNode("for falling through");
mergeForBranching ->setScope(currentScope());
mergeForFallingThrough->setScope(currentScope());
for ( i = 0;
i < exprs->length();
++i) {
SExpr* e = exprs->nth(i);
Node* n = e->node();
MergeNode* mn = e->constant() == targetOop
? mergeForBranching
: mergeForFallingThrough;
mn->setPrev(n);
n->moveNext(preceedingMerge, mn);
}
while (preceedingMerge->nPredecessors())
preceedingMerge->removePrev(preceedingMerge->prev(0));
current = mergeForBranching;
branchCode( targetNode,
isBackwards,
NULL,
NULL,
targetStack,
exprStack,
exprStackPRs,
s);
append(mergeForFallingThrough);
return NULL;
}
开发者ID:ardeujho,项目名称:self,代码行数:72,代码来源:nodeGen.cpp
示例17: field_err
Field *
ProtoInterpreter::letfed_to_graph(SE_List *s, AM *space, Env *env,
bool init)
{
// Parse the input with the beautiful pattern matching language that
// C++ affords us.
if (! ((s->len() >= 3) & (*s)[1]->isList()))
return field_err(s, space, "Malformed letfed expression: " + s->to_str());
vector<SExpr *>::const_iterator iterator = s->args();
SE_List *bindings = &dynamic_cast<SE_List &>(*(*iterator++));
size_t n = bindings->len();
vector<SExpr *> patterns;
vector<SExpr *> initial_expressions;
vector<SExpr *> update_expressions;
for (size_t i = 0; i < n; i++) {
SExpr *binding = (*bindings)[i];
if (!binding->isList())
compile_error(binding, "Malformed letfed binding: " + binding->to_str());
SE_List *binding_list = &dynamic_cast<SE_List &>(*binding);
if (binding_list->len() != 3)
compile_error(binding, "Malformed letfed binding: " + binding->to_str());
SExpr *pattern = (*binding_list)[0];
if (!letfed_pattern_p(pattern))
compile_error(pattern, "Malformed letfed pattern: " + pattern->to_str());
patterns.push_back(pattern);
initial_expressions.push_back((*binding_list)[1]);
update_expressions.push_back((*binding_list)[2]);
}
// Create the environments, conditional OIs, and subspaces.
Env *body_env = new Env(env), *update_env = new Env(env);
OI *true_if_change, *false_if_change;
AM *initial_space, *update_space;
if (init) {
true_if_change = new OI(s, Env::core_op("dchange"), space);
false_if_change = new OI(s, Env::core_op("not"), space);
false_if_change->add_input(true_if_change->output);
initial_space = new AM(s, space, true_if_change->output);
update_space = new AM(s, space, false_if_change->output);
} else {
true_if_change = false_if_change = 0;
initial_space = 0;
update_space = space;
}
// Evaluate the initial expressions.
vector<OI *> ois;
for (size_t i = 0; i < n; i++) {
SExpr *binding = (*bindings)[i];
SExpr *pattern = patterns[i];
SExpr *initial_expression = initial_expressions[i];
OI *delay = new OI(binding, Env::core_op("delay"), update_space);
if (init) {
OI *mux = new OI(binding, Env::core_op("mux"), space);
mux->attributes["LETFED-MUX"] = new MarkerAttribute(true);
mux->add_input(true_if_change->output);
mux->add_input(sexp_to_graph(initial_expression, initial_space, env));
delay->add_input(mux->output);
ois.push_back(mux);
} else {
delay->output->range = sexp_to_type(initial_expression);
ois.push_back(delay);
}
// Bind the pattern variables to the delayed fields in the
// environment for the update expression.
bind_letfed_pattern(pattern, delay->output, update_space, update_env);
}
// Evaluate the update expressions.
for (size_t i = 0; i < n; i++) {
SExpr *binding = (*bindings)[i];
SExpr *pattern = patterns[i];
SExpr *update_expression = update_expressions[i];
Field *update = sexp_to_graph(update_expression, update_space, update_env);
Field *field;
if (init)
field = ois[i]->output;
else
field = update;
// Bind the pattern variables to the actual field in the body.
bind_letfed_pattern(pattern, field, space, body_env);
// Feed the update field back into the mux/delay OI.
ois[i]->add_input(update);
}
// Evaluate the body.
Field *field = 0;
while (iterator != s->children.end())
field = sexp_to_graph(*iterator++, space, body_env);
//.........这里部分代码省略.........
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:101,代码来源:interpreter.cpp
示例18: ce2s
// returns the output field
Field* ProtoInterpreter::sexp_to_graph(SExpr* s, AM* space, Env *env) {
V3 << "Interpret: " << ce2s(s) << " in " << ce2s(space) << endl;
if(s->isSymbol()) {
// All other symbols are looked up in the environment
CompilationElement* elt = env->lookup(dynamic_cast<SE_Symbol &>(*s).name);
if(elt==NULL) {
V4 << "Symbolic literal?\n";
ProtoType* val = symbolic_literal(dynamic_cast<SE_Symbol &>(*s).name);
if(val) { V4 << "- Yes\n"; return dfg->add_literal(val,space,s); }
return field_err(s,space,"Couldn't find definition of "+s->to_str());
} else if(elt->isA("Field")) {
V4 << "Found field: " << ce2s(elt) << endl;
Field* f = &dynamic_cast<Field &>(*elt);
if(f->domain==space) { return f;
} if(f->domain->child_of(space)) {
ierror(s,"Direct reference to child space in parent:"+ce2s(s));
} else { // implicit restriction
OI *oi = new OperatorInstance(s,Env::core_op("restrict"),space);
oi->add_input(f);
if(space->selector) oi->add_input(space->selector);
return oi->output;
}
} else if(elt->isA("Operator")) {
V4 << "Lambda literal: " << ce2s(elt) << endl;
return dfg->add_literal(new ProtoLambda(&dynamic_cast<Operator &>(*elt)),
space, s);
} else if(elt->isA("MacroSymbol")) {
V4 << "Macro: " << ce2s(elt) << endl;
return
sexp_to_graph(dynamic_cast<MacroSymbol &>(*elt).pattern,space,env);
} else return field_err(s,space,"Can't interpret "+elt->type_of()+" "+
s->to_str()+" as field");
} else if(s->isScalar()) { // Numbers are literals
V4 << "Numeric literal.\n";
return
dfg->add_literal(new ProtoScalar(dynamic_cast<SE_Scalar &>(*s).value),
space,s);
} else { // it must be a list
// Lists are special forms or function applicatios
SE_List* sl = &dynamic_cast<SE_List &>(*s);
if(sl->len()==0) return field_err(sl,space,"Expression has no members");
if(sl->op()->isSymbol()) {
// check if it's a special form
string opname = dynamic_cast<SE_Symbol &>(*sl->op()).name;
if(opname=="let") { return let_to_graph(sl,space,env,false);
} else if(opname=="let*") { return let_to_graph(sl,space,env,true);
} else if(opname=="all") { // evaluate children, returning last field
Field* last=NULL;
V4 << "Found 'all' construct\n";
for(int j=1;j<sl->len();j++) last = sexp_to_graph((*sl)[j],space,env);
return last;
} else if(opname=="restrict"){
return restrict_to_graph(sl,space,env);
} else if(opname=="def" && sl->len()==3) { // variable definition
SExpr *def=(*sl)[1], *exp=(*sl)[2];
if(!def->isSymbol())
return field_err(sl,space,"def name not a symbol: "+def->to_str());
Field* f = sexp_to_graph(exp,space,env);
env->force_bind(dynamic_cast<SE_Symbol &>(*def).name,f);
V4 << "Defined variable: " << ce2s(f) << endl;
return f;
} else if(opname=="def" || opname=="primitive" ||
opname=="lambda" || opname=="fun") {
Operator* op = sexp_to_op(s,env);
if(!(opname=="lambda" || opname=="fun")) return NULL;
return dfg->add_literal(new ProtoLambda(op),space,s);
} else if(opname=="annotate") {
SE_List_iter li(sl); li.get_next(); // make iterator, discard op
string name = li.get_token("operator name");
CE* p = env->lookup(name);
if(p==NULL) {
compile_error(sl,"Can't find primitve '"+name+"' to annotate");
} else if(!p->isA("Primitive")) {
compile_error(sl,"Can't annotate '"+name+"': not a primitive");
} else {
// add in attributes
parse_primitive_attributes(&li, &dynamic_cast<Primitive &>(*p));
}
return NULL; // annotations are like primitives: nothing returned
} else if(opname=="letfed" || opname=="letfed+") {
return letfed_to_graph(sl,space,env,opname=="letfed");
} else if(opname=="macro") {
V4 << "Defining macro\n";
sexp_to_macro(sl,env);
return NULL;
} else if(opname=="include") {
for(int j=1;j<sl->len();j++) {
SExpr *ex = (*sl)[j];
V4 << "Including file: "<<ce2s(ex)<<endl;
if(ex->isSymbol())
interpret_file(dynamic_cast<SE_Symbol &>(*ex).name);
else compile_error(ex,"File name "+ex->to_str()+" is not a symbol");
}
return NULL;
} else if(opname=="quote") {
if(sl->len()!=2)
return field_err(sl,space,"Quote requires an argument: "+s->to_str());
V4 << "Creating quote literal\n";
return dfg->add_literal(quote_to_literal_type((*sl)[1]),space,s);
//.........这里部分代码省略.........
开发者ID:chazmatazz,项目名称:proto-mirror,代码行数:101,代码来源:interpreter.cpp
示例19: makeSTATSlog
Log * makeSTATSlog() {
//Log state.
std::vector<SExpr> fields;
fields.push_back(SExpr(CONTENT_TYPE_S, "STATS"));
std::vector<SExpr> fvector = {
SExpr("flags"),
SExpr("serv_connected", control::serv_connected,
control::flags[control::serv_connected]),
SExpr("control_connected", control::control_connected,
control::flags[control::control_connected]),
SExpr("fileio", control::fileio,
control::flags[control::fileio]),
SExpr("SENSORS", control::SENSORS,
control::flags[control::SENSORS]),
SExpr("GUARDIAN", control::GUARDIAN,
control::flags[control::GUARDIAN]),
SExpr("COMM", control::COMM,
control::flags[control::COMM]),
SExpr("LOCATION", control::LOCATION,
control::flags[control::LOCATION]),
SExpr("ODOMETRY", control::ODOMETRY,
control::flags[control::ODOMETRY]),
SExpr("OBSERVATIONS", control::OBSERVATIONS,
control::flags[control::OBSERVATIONS]),
SExpr("LOCALIZATION", control::LOCALIZATION,
control::flags[control::LOCALIZATION]),
SExpr("BALLTRACK", control::BALLTRACK,
control::flags[control::BALLTRACK]),
SExpr("VISION", control::VISION,
control::flags[control::VISION]),
SExpr("tripoint", control::tripoint,
control::flags[control::tripoint]),
SExpr("thumbnail", control::thumbnail,
control::flags[control::thumbnail])
};
fields.push_back(SExpr(fvector));
fields.push_back(SExpr("num_buffers", NUM_LOG_BUFFERS));
fields.push_back(SExpr("num_cores", (int) NUM_CORES));
SExpr ratios;
ratios.append(SExpr("ratio"));
for (int i = 0; i < NUM_LOG_BUFFERS; ++i) {
ratios.append(SExpr(LOG_RATIO[i]));
}
fields.push_back(ratios);
SExpr sizes;
sizes.append(SExpr("size"));
for (int i = 0; i < NUM_LOG_BUFFERS; ++i) {
sizes.append(SExpr(LOG_BUFFER_SIZES[i]));
}
fields.push_back(sizes);
time_t NOW = time(NULL);
fields.push_back(SExpr("con_uptime", control::flags[control::serv_connected] ? difftime(NOW, cio_upstart) : 0));
fields.push_back(SExpr("cnc_uptime", control::flags[control::control_connected] ? difftime(NOW, cnc_upstart) : 0));
fields.push_back(SExpr("fio_uptime", control::flags[control::fileio] ? difftime(NOW, fio_upstart) : 0));
fields.push_back(SExpr("log_uptime", difftime(NOW, main_upstart)));
SExpr manages;
manages.append(SExpr("bufmanage"));
for (int i = 0; i < NUM_LOG_BUFFERS; ++i) {
manages.append(makeBufManage(i));
}
fields.push_back(manages);
/*
This system of grabbing io state is subject to multi-threading accuracy drift.
It is therefore only for estimates.
*/
io_state_t zerostate;
bzero(&zerostate, sizeof(io_state_t));
SExpr state_total;
state_total.append(SExpr("total-state"));
for (int i = 0; i < NUM_LOG_BUFFERS; ++i) {
state_total.append(makeBufState(&zerostate, total + i));
}
fields.push_back(state_total);
if (control::flags[control::fileio]) {
SExpr state_file;
state_file.append(SExpr("file-state"));
for (int i = 0; i < NUM_LOG_BUFFERS; ++i) {
state_file.append(makeBufState(fio_start + i, total + i));
}
fields.push_back(state_file);
}
if (control::flags[control::serv_connected]) {
SExpr state_serv;
state_serv.append(SExpr("serv-state"));
//.........这里部分代码省略.........
开发者ID:MarcusEFC,项目名称:nbites,代码行数:101,代码来源:log_main.cpp
|
请发表评论