/// <summary> /// Retrieve concrete expression of an fieldexpression /// </summary> /// <param name="expression">FieldExpression</param> /// <returns>Concrete expression</returns> private Expression GetExpression(FieldExpression expression) { Expression expr = expression.GetExpression(); //Get real expression, not a variable while (expr is VarExpression) { expr = SymbolTable.GetVariableDefinition(((VarExpression)expr).GetVariableIdentifier()); } //Get specific record from recordExpression if (expr is RecordExpression) { RecordExpression record = (RecordExpression)expr; ISyntaxNode[] recordArray = record.GetRecords().ToArray(); foreach (KeyValuePair pair in recordArray) { if (pair.GetKey() == expression.GetIdentifier()) { return(pair.GetValue()); } } } return(null); }
/// <summary> /// Parse an RecordExpression /// </summary> /// <returns>Parsed Record Expression</returns> public RecordExpression ParseRecordExpression() { RecordExpression recordExpression = new RecordExpression(); //Skip { token NextToken("{", "{key:value, key:value}", '{'); while (TokenStream.HasNext()) { //Scan for key value pairs if (TokenStream.Peek(1).GetValue().ToString() == "}") { break; //} marks end of stream } recordExpression.AddKeyValuePair(ParseKeyValuePair()); if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == ",") { //Skip , token NextToken(",", "{key:value, key:value}", ','); } } //Skip } token NextToken("}", "{key:value, key:value}", '}'); return(recordExpression); }
public void ParseRecordExpressionTest() { //Create parser and parse tokens ExpressionParser expressionParser = new ExpressionParser(Init("{token1:\"token1\", token2:10, token3:'symbol}")); RecordExpression expression = expressionParser.ParseRecordExpression(); //Test Record contents Assert.AreEqual(3, expression.GetRecords().Count); List <ISyntaxNode> .Enumerator recordEnumerator = expression.GetRecords().GetEnumerator(); recordEnumerator.MoveNext(); KeyValuePair current = (KeyValuePair)recordEnumerator.Current; Assert.AreEqual("token1", current.GetKey()); Assert.AreEqual(typeof(TextExpression), current.GetValue().GetType()); Assert.AreEqual("\"token1\"", current.GetValue().ToString()); recordEnumerator.MoveNext(); current = (KeyValuePair)recordEnumerator.Current; Assert.AreEqual("token2", current.GetKey()); Assert.AreEqual(typeof(NumExpression), current.GetValue().GetType()); Assert.AreEqual(10, ((NumExpression)current.GetValue()).GetNum()); recordEnumerator.MoveNext(); current = (KeyValuePair)recordEnumerator.Current; Assert.AreEqual("token3", current.GetKey()); Assert.AreEqual(typeof(SymExpression), current.GetValue().GetType()); Assert.AreEqual("'symbol", current.GetValue().ToString()); }
ExpressionType TranslateExpression(RecordExpression e) { Types.Type eType = Env.TypeEnvironment[e.Type] as Types.Type; if (eType == null) { Error.Report(e.Pos, "Undefined record type " + e.Type.ToString()); return(new ExpressionType(null, Types.Type._int)); } eType = eType.Actual; if (!(eType is Types.RECORD)) { Error.Report(e.Pos, "Record type required"); return(new ExpressionType(null, Types.Type._int)); } FieldExpressionList eFields = e.Fields; Types.RECORD eRecord = (Types.RECORD)eType; ExpressionType et; List <Exp> fieldList = new List <Exp>(); for (; eFields != null; eFields = eFields.Tail, eRecord = eRecord.Tail) { if (eRecord == null) { Error.Report(eFields.Pos, "Field " + eFields.Name.ToString() + " has not been declared"); break; } if (eRecord.FieldName != eFields.Name) { Error.Report(eFields.Pos, eRecord.FieldName.ToString() + " field dismatch"); break; } et = TranslateExpression(eFields.Init); fieldList.Add(et.Exp); if (!et.Type.CoerceTo(eRecord.FieldType)) { Error.Report(eFields.Pos, "Type mismatch in record field"); } } if (eRecord != null) { Error.Report(eFields.Pos, "Missing record fields"); } return(new ExpressionType(Translate.TranslateRecordExp(Level, fieldList), eType)); }
/// <summary> /// Interpret RecordExpression /// </summary> /// <param name="expression">RecordExpression to interpret</param> public override void Visit(RecordExpression expression) { //convert record to textvalue String tempRecord = "{"; for (int i = 0; i < expression.GetRecords().Count; i++) { KeyValuePair pair = (KeyValuePair)expression.GetRecords().Get(i); //Convert KeyValuePair to textual representation tempRecord += pair.GetKey() + ":"; pair.GetValue().AcceptVisitor(this); tempRecord += TextValue; //Add seperator if (i != (expression.GetRecords().Count - 1)) { tempRecord += ","; } } tempRecord += "}"; TextValue = tempRecord; }
public TypeDenoter VisitRecordExpression(RecordExpression ast, Void arg) { var recordType = ast.RecordAggregate.Visit(this); return(ast.Type = new RecordTypeDenoter(recordType, ast.Position)); }
void PrintExpression(RecordExpression e, int d) { Say("RecordExpression("); Say(e.Type.ToString()); SayLn(","); PrintFieldExpressionList(e.Fields, d + 1); Say(")"); }
public virtual void Visit(RecordExpression expression) { VisitSubNodes(expression); }
public int VisitRecordExpression(RecordExpression ast, Frame frame) { ast.Type.Visit(this, null); return(ast.RecordAggregate.Visit(this, frame)); }