private static void CompileMemoryFunction(int level, bool debug, Node node, Node container, NanCodeWriter wr, Scope parameterNames) { // Check for special increment mode if (node.Text == "set" && Optimisations.IsSmallIncrement(node, out var incr, out var target)) { wr.Increment(incr, target); return; } var child = new Node(false, -2) { Text = container.Children.First.Value.Text }; var paramCount = container.Children.Count - 1; for (int i = paramCount; i > 0; i--) { child.AddLast(container.Children.ElementAt(i)); } wr.Merge(Compile(child, level + 1, debug, parameterNames, null, Context.MemoryAccess)); if (debug) { wr.Comment("// Memory function : " + node.Text); } wr.Memory(node.Text[0], child.Text, paramCount); }
/// <summary> /// Output atom values /// </summary> private static void EmitLeafNode(Node root, bool debug, Scope parameterNames, Context compileContext, NanCodeWriter wr) { double leafValue = 0; bool substitute = false; var valueName = root.Text; var nameHash = NanTags.GetCrushedName(valueName); if (parameterNames.CanResolve(nameHash)) { substitute = true; leafValue = parameterNames.Resolve(nameHash); } // An unwrapped variable name? if (IsUnwrappedIdentifier(valueName, root, compileContext)) { if (debug) { wr.Comment("// treating '" + valueName + "' as an implicit get()"); } if (substitute) { wr.Memory('g', leafValue); } else { wr.Memory('g', valueName, 0); } return; } if (debug) { wr.Comment("// Value : \"" + root + "\"\r\n"); if (substitute) { wr.Comment("// Parameter reference redefined as '" + valueName + "'\r\n"); } } switch (root.NodeType) { case NodeType.Numeric: wr.LiteralNumber(double.Parse(valueName.Replace("_", ""))); break; case NodeType.StringLiteral: wr.LiteralString(valueName); break; case NodeType.Atom: if (valueName == "true") { wr.LiteralInt32(-1); } else if (valueName == "false") { wr.LiteralInt32(0); } else if (substitute) { wr.RawToken(leafValue); } else { wr.VariableReference(valueName); } break; default: throw new Exception("Unexpected compiler state"); } }