// Assignment:: VAR? ident := value // If ident does not exist, creates a typed catalog entry (using return type if Funval) // Sets initial value, or updates but only if was VAR and same type public AstStatement Assignment(string ident, AstValue value, string varopt) { if (Symbols.CanDefGlobal(ident)) { if (value is AstFunval) { var cvalue = (value as AstFunval).Value; Symbols.AddDeffun(ident, cvalue.DataType, cvalue.Lookup.Columns, cvalue.Accums, varopt != null); } else { Symbols.AddVariable(ident, value.DataType, SymKinds.CATVAR, varopt != null); } Symbols.AddCatalog(Symbols.FindIdent(ident)); } else { var sym = FindCatVar(ident); if (sym == null || sym.IsCallable || !sym.Mutable) { Parser.ParseError("cannot assign to '{0}'", sym.Name); } if (sym.DataType != value.DataType) { Parser.ParseError("type mismatch: '{0}'", sym.Name); } } return(GenFunCall(FindFunc(SymNames.Assign), Args(Text(ident), value))); }
// UPDATE rel <set op> relexpr public AstStatement UpdateSetop(AstValue relvar, string joinop, AstValue expr) { var joinsym = FindFunc(joinop); var joinnum = Number((int)joinsym.JoinOp); return(FunCall(FindFunc(SymNames.UpdateJoin), DataTypes.Void, Args(relvar, expr, joinnum))); }
// Call user function from expression value (no callinfo) // Type check against FullDataType AstDefCall FunvalCall(AstValue value, DataTypeCode datatype, AstNode[] args) { return(new AstDefCall { Func = FindFunc(SymNames.Invoke), Code = value, DataType = datatype.Returns, Arguments = args, NumVarArgs = args.Length, }); }
// Handle Binop with precedence AstValue Binop(AstValue value, AstOpCall[] ops) { Logger.WriteLine(4, "expr {0} op={1}", value, ops.Join(",")); if (ops.Length == 0) { return(value); } var op = ops[0]; if (ops.Length == 1) { return(GenFunCall(op.Func, Args(value, op.Arguments))); } var opnext = ops[1]; var optail = ops.Skip(1); //Func<AstCall, AstCall, bool> op high = (op1, op2) => !op1.Func.IsOperator || op1.Func.Precedence >= op2.Func.Precedence; if (!op.Func.IsOperator || op.Func.Precedence >= opnext.Func.Precedence) { return(Binop(GenFunCall(op.Func, Args(value, op.Arguments)), optail.ToArray())); } // The hard case -- rewrite the tree // extract higher precedence ops and do those first; then lower var hiprec = optail.TakeWhile(o => !o.Func.IsOperator || o.Func.Precedence > op.Func.Precedence); var hivalue = Binop(op.Arguments[0] as AstValue, hiprec.ToArray()); var loprec = optail.SkipWhile(o => !o.Func.IsOperator || o.Func.Precedence > op.Func.Precedence); return(Binop(GenFunCall(op.Func, Args(value, hivalue)), loprec.ToArray())); }
private void GenerateValueCode(AstValue valueInfo) { //if (valueInfo.IsIntegerValue) // ActiveFileComposer.Append(valueInfo.IntegerValue.ToString()); //else if (valueInfo.IsBooleanValue) // ActiveFileComposer.Append(valueInfo.BooleanValue.ToString()); if (valueInfo.IsValidScalarValue) { GenerateScalarValueCode(valueInfo as AstValueScalar); } else if (valueInfo.IsValidMultivectorValue) { GenerateMultivectorValueCode(valueInfo as AstValueMultivector); } else if (valueInfo.IsValidStructureValue) { GenerateStructureValueCode(valueInfo as AstValueStructure); } else { ActiveFileTextComposer.Append("<undefined value>"); } }
internal GMacScriptOutputItem(string title, string description, AstValue item) { ItemType = OutputItemType.Value; Item = item; Title = title; Description = description; }
///-------------------------------------------------------------------------------------------- ///--- internal functions /// // Code node: Datatype used as return value AstCode Code(AstValue value, DataHeading lookup = null, string name = "&c", bool ascode = false, int accums = -1, bool hasord = false) { return(new AstCode { Name = name, Value = value, DataType = value.DataType, Lookup = lookup, Accums = accums, AsCode = ascode, HasWin = hasord, }); }
public GMacScriptOutputItem Store(string title, string description, AstValue item) { var outputItem = new GMacScriptOutputItem(title, description, item); Items.Add(outputItem); return(outputItem); }
public GMacScriptOutputItem Store(string title, AstValue item) { var outputItem = new GMacScriptOutputItem(title, String.Empty, item); Items.Add(outputItem); return(outputItem); }
// Relation by conversion public AstValue Table(AstValue value) { if (!value.DataType.HasHeading) { Parser.ParseError("value has no heading"); } return(new AstTabCall { Func = FindFunc(SymNames.TableC), DataType = Types.Relof(value.DataType), Arguments = Args(value), }); }
public AstValue Row(AstValue value) { if (!value.DataType.HasHeading) { Parser.ParseError("value has no heading"); } return(new AstTabCall { Func = FindFunc(SymNames.RowC), DataType = DataTypeTuple.Get(value.DataType.Heading), Arguments = Args(value), }); }
// function call on anon function Funval public AstFunCall FunCall(AstValue expr, params AstValue[] args) { var code = expr as AstFunval; if (code == null) { Parser.ParseError($"callable expression expected"); } return(FunvalCall(code.Value, code.DataType as DataTypeCode, args)); }
public void VisitValue(AstValue value) { Assert.IsType <AstValue>(this.expected); var expectedAst = (AstValue)this.expected; Assert.True(expectedAst.Expressions.Length == value.Expressions.Length, "Expected length of expressions is not equal to actual"); for (int i = 0; i < value.Expressions.Length; i++) { AssertVisit(expectedAst.Expressions[i], value.Expressions[i]); } }
// translate transform calls on tuple into a function call AstValue PostFixTranTup(AstValue value, AstTransformer tran) { Types.CheckTypeMatch(DataTypes.Row, value.DataType); if (tran == null) { return(value); } var args = Args(value, tran.Elements); return(FunCall(FindFunc(SymNames.TransTuple), tran.DataType, args, args.Length - 1)); }
// Where is just a funcall with a code predicate (that may have a fold) public AstWhere Where(string name, AstValue predicate) { var lookup = DataHeading.Create(Symbols.CurrentScope.LookupItems.Items); var code = Code(predicate, lookup, "&p", false, _accum.Total, _accum.HasWin); _accum = _accum.Push(); return(new AstWhere { Func = FindFunc(name), DataType = Types.Relof(CurrentHeading()), Arguments = Args(code), }); }
/// <summary> /// Return a simple string to describe the given value /// </summary> /// <param name="value"></param> /// <returns></returns> public string AsString(AstValue value) { if (value.IsValidMultivectorValue) { return(AsString((AstValueMultivector)value)); } //TODO: Change this for structures if (value.IsNullOrInvalid()) { return("<Invalid Value>"); } return(value.ToString()); }
public AstOpCall While(string name, AstValue expr) { var datatype = Types.Relof(CurrentHeading()); if (expr.DataType != datatype) { Parser.ParseError("type mismatch"); } return(new AstOpCall() { Func = FindFunc(name), DataType = Types.Relof(CurrentHeading()), Arguments = Args(Number(0), expr), }); }
// UPDATE rel .where(pred) .{ sets } public AstStatement UpdateWhere(AstValue relvar, AstValue wherearg, AstTransformer tran) { var where = (wherearg != null) ? (wherearg as AstWhere).Arguments[0] : null; // null means delete if (tran == null) { return(FunCall(FindFunc(SymNames.UpdateTransform), DataTypes.Void, Args(relvar, Args(where)), 0)); } if (tran.DataType.Heading != relvar.DataType.Heading) { Parser.ParseError("field mismatch"); } return(FunCall(FindFunc(SymNames.UpdateTransform), DataTypes.Void, Args(relvar, Args(where, tran.Elements)), tran.Elements.Length)); }
// handle generic transform rule and create specific node with all required info // each element tracks lookup items and folds public AstField Transfield(string name, string rename = null, AstValue value = null) { // Add symbols that were never referenced as variables if (rename != null) // Rename { Symbols.CurrentScope.LookupItems.Add(DataColumn.Create(rename, FindField(rename).DataType)); } else if (value == null) // Project { Symbols.CurrentScope.LookupItems.Add(DataColumn.Create(name, FindField(name).DataType)); } var lookup = DataHeading.Create(Symbols.CurrentScope.LookupItems.Items); //Symbols.CurrentScope.LookupItems.Clear(); var accums = _accum.Count; var haswin = _accum.HasWin; _accum.Reset(false); if (name == null) { return new AstLift { Name = "^", Value = value, DataType = value.DataType, Lookup = lookup, Accums = accums, HasWin = haswin, } } ; if (rename != null) { return new AstRename { Name = name, OldName = rename, DataType = FindField(rename).DataType } } ; if (value != null) { return new AstExtend { Name = name, DataType = value.DataType, Value = value, Lookup = lookup, Accums = accums, HasWin = haswin, } } ; return(new AstProject { Name = name, DataType = FindField(name).DataType }); }
public AstFoldCall Fold(string name, string oper, AstValue expression) { var op = FindFunc(oper); DataType datatype; CallInfo callinfo; Types.CheckTypeError(op, out datatype, out callinfo, expression.DataType, expression.DataType); // as if there were two if (datatype != expression.DataType) { Parser.ParseError($"not foldable: {name}"); } var accndx = _accum.Total; _accum.Add(1); return(new AstFoldCall { Func = FindFunc(name), FoldedOp = op, DataType = datatype, AccumIndex = accndx, FoldedExpr = expression, CallInfo = callinfo, InvokeOp = (op.IsDefFunc) ? Symbols.FindIdent(SymNames.Invoke) : null, }); }
// translate transform call and possible order into a function call // note that tran may be empty, and this has meaning! AstValue PostFixTranRel(AstValue value, AstTransformer tran, AstOrderer order) { Types.CheckTypeMatch(DataTypes.Table, value.DataType); var args = new List <AstNode> { value }; if (order != null) { args.AddRange(order.Elements); } if (tran != null) { args.AddRange(tran.Elements); } if (tran != null) { // transformation, possibly ordered // Decide which processor to use depending on level of elements var opname = tran.Elements.Any(e => e is AstExtend && (e as AstExtend).HasWin) ? SymNames.TransWin : (order != null) ? SymNames.TransOrd : tran.Elements.Any(e => e is AstExtend && (e as AstExtend).Accums > 0) ? SymNames.TransAgg : tran.Elements.Any(e => e is AstExtend) ? SymNames.Transform : tran.Elements.All(e => e is AstRename) && tran.Elements.Length == value.DataType.Heading.Degree ? SymNames.Rename : SymNames.Project; return(FunCall(FindFunc(opname), tran.DataType, args.ToArray(), args.Count - 1)); } else if (order != null) { // just a sort, convenient way to include all fields in heading args.AddRange(Allbut(value.DataType.Heading, new AstField[0])); return(FunCall(FindFunc(SymNames.TransOrd), value.DataType, args.ToArray(), args.Count - 1)); } else { return(value); // nothing do to } }
//NOTE: these two methods need to be moved to the ascx_SourceCodeEditor control public void showInSourceCode(Object sender, TreeViewEventArgs e) { var treeNoteTag = e.Node.Tag; // temp Hack to handle the fact that AstValue<obTject> is current using AstValue<object> var endLocation = (ICSharpCode.NRefactory.Location)O2.Kernel.PublicDI.reflection.getProperty("EndLocation", treeNoteTag); var startLocation = (ICSharpCode.NRefactory.Location)O2.Kernel.PublicDI.reflection.getProperty("StartLocation", treeNoteTag); var originalObject = O2.Kernel.PublicDI.reflection.getProperty("OriginalObject", treeNoteTag); var text = (string)O2.Kernel.PublicDI.reflection.getProperty("Text", treeNoteTag); var astValue = new AstValue <object>(text, originalObject, startLocation, endLocation); //if (treeNoteTag is AstValue<object>) //{ //var astValue = (AstValue<object>)treeNoteTag; var textEditorControl = sourceCodeEditor.getObject_TextEditorControl(); var start = new ICSharpCode.TextEditor.TextLocation(astValue.StartLocation.X - 1, astValue.StartLocation.Y - 1); var end = new ICSharpCode.TextEditor.TextLocation(astValue.EndLocation.X - 1, astValue.EndLocation.Y - 1); var selection = new ICSharpCode.TextEditor.Document.DefaultSelection(textEditorControl.Document, start, end); textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(selection); setCaretToCurrentSelection(textEditorControl); //} }
/// <summary> /// Bind a macro parameter of any type to a constant of the same type /// </summary> /// <param name="valueAccess"></param> /// <param name="value"></param> /// <returns></returns> public GMacMacroBinding BindToConstants(AstDatastoreValueAccess valueAccess, AstValue value) { if (valueAccess.IsNullOrInvalid()) { throw new ArgumentNullException(nameof(valueAccess)); } //This is checked inside each primitive binding to select appropriate action according to the // BindOutputToConstantBehavior mamber //if (valueAccess.IsInputParameter == false) // throw new InvalidOperationException( // String.Format( // "Specified value access {0} is not a macro input parameter", // valueAccess.ValueAccessName // ) // ); if (valueAccess.AssociatedValueAccess.ExpressionType.IsSameType(value.AssociatedValue.ExpressionType) == false) { throw new InvalidOperationException( $"Specified macro parameter {valueAccess.ValueAccessName} of type {valueAccess.GMacTypeSignature} is not of same type {value.GMacTypeSignature} as given value" ); } var assignmentsList = valueAccess.AssociatedValueAccess.ExpandAndAssignAll(value.AssociatedValue); foreach (var assignment in assignmentsList) { BindScalarToConstant( assignment.Item1.ToAstDatastoreValueAccess(), assignment.Item2.ToExpr() ); } return(this); }
///-------------------------------------------------------------------------------------------- /// Calls with arguments /// // Handle a list of infix operators public AstValue Binop(AstValue value, IList <AstOpCall> ops) { var ret = Binop(value, ops.ToArray()); return(ret); }
//NOTE: these two methods need to be moved to the ascx_SourceCodeEditor control public void showInSourceCode(Object sender, TreeViewEventArgs e) { var treeNoteTag = e.Node.Tag; // temp Hack to handle the fact that AstValue<obTject> is current using AstValue<object> var endLocation = (ICSharpCode.NRefactory.Location)O2.Kernel.PublicDI.reflection.getProperty("EndLocation",treeNoteTag); var startLocation = (ICSharpCode.NRefactory.Location)O2.Kernel.PublicDI.reflection.getProperty("StartLocation", treeNoteTag); var originalObject = O2.Kernel.PublicDI.reflection.getProperty("OriginalObject", treeNoteTag); var text = (string)O2.Kernel.PublicDI.reflection.getProperty("Text", treeNoteTag); var astValue = new AstValue<object>(text,originalObject,startLocation,endLocation); //if (treeNoteTag is AstValue<object>) //{ //var astValue = (AstValue<object>)treeNoteTag; var textEditorControl = sourceCodeEditor.getObject_TextEditorControl(); var start = new ICSharpCode.TextEditor.TextLocation(astValue.StartLocation.X - 1, astValue.StartLocation.Y - 1); var end = new ICSharpCode.TextEditor.TextLocation(astValue.EndLocation.X - 1, astValue.EndLocation.Y - 1); var selection = new ICSharpCode.TextEditor.Document.DefaultSelection(textEditorControl.Document, start, end); textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(selection); setCaretToCurrentSelection(textEditorControl); //} }
public static TextEditorControl showAstValueInSourceCode(this TextEditorControl textEditorControl, AstValue <object> astValue) { return((TextEditorControl)textEditorControl.invokeOnThread(() => { PublicDI.log.error("{0} {1} - {2}", astValue.Text, astValue.StartLocation, astValue.EndLocation); var start = new TextLocation(astValue.StartLocation.X - 1, astValue.StartLocation.Y - 1); var end = new TextLocation(astValue.EndLocation.X - 1, astValue.EndLocation.Y - 1); var selection = new DefaultSelection(textEditorControl.Document, start, end); textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(selection); setCaretToCurrentSelection(textEditorControl); return textEditorControl; })); }
public AstFunCall If(string name, AstValue condition, AstValue iftrue, AstValue iffalse) { Types.CheckTypeMatch(iftrue.DataType, iffalse.DataType); return(FunCall(FindFunc(name), iftrue.DataType, Args(condition, Code(iftrue), Code(iffalse)))); }
public AstType GetType(AstValue value) { return(new AstType { DataType = value.DataType }); }
public static TextEditorControl showAstValueInSourceCode(this TextEditorControl textEditorControl, AstValue<object> astValue) { return (TextEditorControl)textEditorControl.invokeOnThread(() => { PublicDI.log.error("{0} {1} - {2}", astValue.Text, astValue.StartLocation, astValue.EndLocation); var start = new TextLocation(astValue.StartLocation.X - 1, astValue.StartLocation.Y - 1); var end = new TextLocation(astValue.EndLocation.X - 1, astValue.EndLocation.Y - 1); var selection = new DefaultSelection(textEditorControl.Document, start, end); textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(selection); setCaretToCurrentSelection(textEditorControl); return textEditorControl; }); }
private void showInSourceCode(object sender, TreeViewEventArgs e) { var treeNoteTag = e.Node.Tag; var endLocation = (ICSharpCode.NRefactory.Location)PublicDI.reflection.getProperty("EndLocation",treeNoteTag); var startLocation = (ICSharpCode.NRefactory.Location)PublicDI.reflection.getProperty("StartLocation", treeNoteTag); var originalObject = PublicDI.reflection.getProperty("OriginalObject", treeNoteTag); var text = (string)PublicDI.reflection.getProperty("Text", treeNoteTag); var astValue = new AstValue<object>(text,originalObject,startLocation,endLocation); textEditorControl.showAstValueInSourceCode(astValue); //astValue.EndLocation = /*if (treeNoteTag is AstValue<ICSharpCode.NRefactory.Ast.MethodDeclaration>) textEditorControl.showAstValueInSourceCode((AstValue<ICSharpCode.NRefactory.Ast.MethodDeclaration>)treeNoteTag); else if (treeNoteTag is AstValue<object>) { var astValue = (AstValue<object>)treeNoteTag; //var textEditorControl = sourceCodeEditor.getObject_TextEditorControl(); textEditorControl.showAstValueInSourceCode(astValue); }*/ }
// Handle a list of postfix operators // construct a FunCall from the first OpCall (or first two), then invoke tail on result public AstValue PostFix(AstValue value, IList <AstCall> ops) { if (ops.Count == 0) { return(value); } var newvalue = value; var tail = ops.Skip(1).ToList(); // special handling for Transformer, possibly preceded by Orderer if (ops[0] is AstTransformer) { var tran = ops[0] as AstTransformer; if (value.DataType is DataTypeRelation) { newvalue = PostFixTranRel(value, tran as AstTransformer, null); } else if (value.DataType is DataTypeTuple) { newvalue = PostFixTranTup(value, tran as AstTransformer); } else { Parser.ParseError("relation or tuple type expected"); } if (tran.Lift) { newvalue = FunCall(FindFunc(SymNames.Lift), tran.DataType, Args(newvalue)); } } else if (ops[0] is AstOrderer) { if (!(value.DataType is DataTypeRelation)) { Parser.ParseError("relation type expected"); } if (tail.Count > 0 && tail[0] is AstTransformer) { var tran = tail[0] as AstTransformer; newvalue = PostFixTranRel(value, tran as AstTransformer, ops[0] as AstOrderer); if (tran.Lift) { newvalue = FunCall(FindFunc(SymNames.Lift), tran.DataType, Args(newvalue)); } tail = tail.Skip(1).ToList(); } else { newvalue = PostFixTranRel(value, null, ops[0] as AstOrderer); } } else if (ops[0] is AstFunvalCall) { var op = ops[0] as AstFunvalCall; if (value.DataType as DataTypeCode == null) { Parser.ParseError("operator type expected"); } newvalue = FunvalCall(value, value.DataType as DataTypeCode, op.Arguments); } else { var op = ops[0] as AstOpCall; newvalue = GenFunCall(op.Func, Args(value, op.Arguments)); } return(PostFix(newvalue, tail)); }
// Enter scope for a transform, with accumulator tracking public bool Enter(AstValue value) { Symbols.CurrentScope.Push(value.DataType); _accum = _accum.Push(); return(true); }
/// <summary> /// Bind a macro parameter of any type to a constant of the same type /// </summary> /// <param name="valueAccessName"></param> /// <param name="value"></param> public GMacMacroBinding BindToConstants(string valueAccessName, AstValue value) { var valueAccess = ToValueAccess(valueAccessName, value.AssociatedValue.ExpressionType); return(BindToConstants(valueAccess, value)); }