private static Formule formuleFromPred(Node pred) { Node actualPred = pred.GetChildAt(0); String strPred = formule_str.Substring(actualPred.StartColumn - 1, actualPred.EndColumn - actualPred.StartColumn + 1); List<Formule> args = new List<Formule>(); if (String.Equals(pred.Name, "PRED1")) { Node arg1 = pred.GetChildAt(2).GetChildAt(0); args.Add(new Formule( String.Equals(arg1.Name, "VAR") ? Formule.Formule_Type.variable : Formule.Formule_Type.constante, "" + formule_str[arg1.StartColumn - 1], null)); return new Formule(Formule.Formule_Type.predicat, strPred, args); } else if(String.Equals(pred.Name, "PRED2")) { Node arg1 = pred.GetChildAt(2).GetChildAt(0); Node arg2 = pred.GetChildAt(4).GetChildAt(0); args.Add(new Formule( String.Equals(arg1.Name, "VAR") ? Formule.Formule_Type.variable : Formule.Formule_Type.constante, "" + formule_str[arg1.StartColumn-1], null)); args.Add(new Formule( String.Equals(arg2.Name, "VAR") ? Formule.Formule_Type.variable : Formule.Formule_Type.constante, "" + formule_str[arg2.StartColumn-1], null)); return new Formule(Formule.Formule_Type.predicat, strPred, args); } else /* PRED3 */ { Node arg1 = pred.GetChildAt(2).GetChildAt(0); Node arg2 = pred.GetChildAt(4).GetChildAt(0); Node arg3 = pred.GetChildAt(6).GetChildAt(0); args.Add(new Formule( String.Equals(arg1.Name, "VAR") ? Formule.Formule_Type.variable : Formule.Formule_Type.constante, "" + formule_str[arg1.StartColumn - 1], null)); args.Add(new Formule( String.Equals(arg2.Name, "VAR") ? Formule.Formule_Type.variable : Formule.Formule_Type.constante, "" + formule_str[arg2.StartColumn - 1], null)); args.Add(new Formule( String.Equals(arg3.Name, "VAR") ? Formule.Formule_Type.variable : Formule.Formule_Type.constante, "" + formule_str[arg3.StartColumn - 1], null)); return new Formule(Formule.Formule_Type.predicat, strPred, args); } }
private static Formule parseNode(Node n) { if(String.Equals(n.Name, "FORMULE")) { Node child1 = n.GetChildAt(0); List<Formule> list = new List<Formule>(); if (String.Equals(child1.Name, "NOT")) { if (n.GetChildAt(1).GetChildAt(0).Name == "PREDICAT") /* Le not s'applique seulement au predicat qui le suit */ { Formule f = parseNode(n.GetChildAt(1)); return f.encapsuleNot(); } else /* Le not s'applique sur tout le reste de la formule */ { list.Add(parseNode(n.GetChildAt(1))); return new Formule(Formule.Formule_Type.neg, "empty", list); } } else if(String.Equals(child1.Name, "FORALL")) { Node varNode = n.GetChildAt(1); list.Add(new Formule(Formule.Formule_Type.variable, ""+formule_str[varNode.StartColumn-1], null)); list.Add(parseNode(n.GetChildAt(2))); return new Formule(Formule.Formule_Type.forall, "empty", list); } else if(String.Equals(child1.Name, "EXISTS")) { Node varNode = n.GetChildAt(1); list.Add(new Formule(Formule.Formule_Type.variable, "" + formule_str[varNode.StartColumn-1], null)); list.Add(parseNode(n.GetChildAt(2))); return new Formule(Formule.Formule_Type.exists, "empty", list); } else if(String.Equals(child1.Name, "PREDICAT")) { Node pred = child1.GetChildAt(0); Formule formPred = formuleFromPred(pred); if(n.GetChildCount() == 1) { return formPred; } else { Node child2 = n.GetChildAt(1); Node connecteur = child2.GetChildAt(0); Formule.Formule_Type type; if (String.Equals(connecteur.Name, "AND")) type = Formule.Formule_Type.and; else if (String.Equals(connecteur.Name, "OR")) type = Formule.Formule_Type.or; else /* IMPLY */ type = Formule.Formule_Type.impl; list.Add(formPred); list.Add(parseNode(child2.GetChildAt(1))); return new Formule(type, "empty", list); } } else /* equals "(" */ { Node child3 = n.GetChildAt(2); Node connecteur = child3.GetChildAt(0); if(connecteur.Name == "RIGHT_PAREN") { return parseNode(n.GetChildAt(1)); } /* Si connecteur */ Formule.Formule_Type type; if (String.Equals(connecteur.Name, "AND")) type = Formule.Formule_Type.and; else if (String.Equals(connecteur.Name, "OR")) type = Formule.Formule_Type.or; else /* IMPLY */ type = Formule.Formule_Type.impl; list.Add(parseNode(n.GetChildAt(1))); list.Add(parseNode(child3.GetChildAt(1))); return new Formule(type, "empty", list); } } Console.WriteLine("Erreur cas inconnu FormuleFactory"); return null; }
public STObject GetWordArrayLiteral(Node literal) { // word_array = '#' '(' // ( IDENT | STRING | NUM | CHAR | '#' ( IDENT | word_array ) ) // ')' ; List<object> values = new List<object>(); for (int i = 2, max = literal.GetChildCount() - 1; i < max; ++i) { Node child = literal.GetChildAt(i); if (child.Name == "IDENT") values.Add (STSymbol.Get((child as Token).Image)); else if (child.Name == "STRING") values.Add (GetStringLiteral(child as Token)); else if (child.Name == "NUM") values.Add (GetNumberLiteral(child as Token)); else if (child.Name == "CHAR") values.Add (GetCharLiteral(child as Token)); else if (child.Name == "HASH") { child = literal.GetChildAt(++i); if (child.Name == "IDENT") values.Add (STSymbol.Get((child as Token).Image)); else if (child.Name == "word_array") values.Add (GetWordArrayLiteral(child)); else throw new Exception ("Unhandled path in grammar"); } else throw new Exception ("Unhandled path in grammar"); } return new Irontalk.Array(values.ToArray()); }
public STObject EvaluateSimpleSend(STObject receiver, Node simpleSend, Context context) { var child = simpleSend.GetChildAt(0); if (child.Name == "unary_send") return EvaluateUnarySend(receiver, child, context); else if (child.Name == "binary_send") return EvaluateBinarySend(receiver, child, context); else throw new NotImplementedException("Unhandled grammar production within 'simple_send'"); }
public STObject EvaluateUnarySend(STObject receiver, Node unarySend, Context context) { receiver = receiver.Dereference(); return receiver.Send(STSymbol.Get((unarySend.GetChildAt(0) as Token).Image)); }
public STObject EvaluateReceiver(Node receiver, Context context) { Node child = receiver.GetChildAt(0); if (child.Name == "IDENT") { return new STVariable((receiver[0] as Token).Image, context); //return context.GetVariable((receiver.GetChildAt(0) as Token).Image); } else if (child.Name == "NUM") { return GetNumberLiteral(child as Token); } else if (child.Name == "CHAR") { return GetCharLiteral(child as Token); } else if (child.Name == "symbol") { return GetSymbolLiteral(child); } else if (child.Name == "STRING") { return GetStringLiteral(child as Token); } else if (child.Name == "word_array") { return GetWordArrayLiteral(child); } else if (child.Name == "LEFT_PAREN") { return EvaluateExpression(receiver.GetChildAt(1), context); } else if (child.Name == "block") { return new STBlock (child, context, this); } else if (child.Name == "array") { return GetArrayLiteral(child, context); } return STUndefinedObject.Instance; }
public STObject EvaluateSend(STObject receiver, Node message, Context context) { Node child = message.GetChildAt(0); if (child.Name == "simple_send") return EvaluateSimpleSend(receiver, child, context); else if (child.Name == "keyword_send") return EvaluateKeywordSend(receiver, child, context); else if (child.Name == "assign_send") return EvaluateAssignSend(receiver, child, context); throw new Exception("Should not reach"); }
public STObject EvaluateKeywordSend(STObject receiver, Node keywordSend, Context context) { receiver = receiver.Dereference(); // keyword_send = ( KEYWORD receiver simple_send* )+; string keyword = null; STObject parm = null; StringBuilder selectorBuffer = new StringBuilder(); List<STObject> parms = new List<STObject>(); for (int i = 0, max = keywordSend.GetChildCount(); i < max; ++i) { var item = keywordSend.GetChildAt(i); if (item.Name == "KEYWORD") { if (keyword != null) { selectorBuffer.Append(keyword); parms.Add (parm); } keyword = (item as Token).Image; parm = EvaluateReceiver(keywordSend.GetChildAt(++i), context).Dereference(); } else if (item.Name == "simple_send") { parm = EvaluateSimpleSend(parm, item, context); } } selectorBuffer.Append(keyword); parms.Add (parm); STSymbol selector = STSymbol.Get(selectorBuffer.ToString()); return receiver.Send(selector, parms.ToArray()); }
public STObject EvaluateExpression(Node expression, Context context) { STObject receiver = EvaluateReceiver (expression.GetChildAt(0), context); for (int i = 1, max = expression.GetChildCount(); i < max; ++i) { receiver = EvaluateSend(receiver, expression.GetChildAt(i), context); } receiver = receiver.Dereference(); return receiver; }
public STObject EvaluateBinarySend(STObject receiver, Node binarySend, Context context) { STObject other = EvaluateReceiver(binarySend.GetChildAt(1), context).Dereference(); receiver = receiver.Dereference(); for (int i = 2, max = binarySend.GetChildCount(); i < max; ++i) other = EvaluateUnarySend(other, binarySend.GetChildAt(i), context); return receiver.Send(STSymbol.Get((binarySend.GetChildAt(0) as Token).Image), other); }
public STObject EvaluateAssignSend(STObject receiver, Node assignSend, Context context) { STObject other = EvaluateExpression(assignSend.GetChildAt(1), context).Dereference(); var variable = receiver as STVariable; if (variable == null) throw new Exception ("Error: can only assign to a valid variable lvalue"); variable.Set(other); return other; }
public STObject Evaluate(Node sequence, Context context) { int start = 0; if (sequence.GetChildAt(start).Name == "var_def") ++start; STObject last = STUndefinedObject.Instance; for (int i = start, max = sequence.Count; i < max; ++i) { var node = sequence[i]; if (node.Name == "DOT") continue; last = EvaluateStatement (node, context); } return last; }