using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShintenScript { public class ASTNodeBlock : IASTNode { public IList statements; public ASTNodeBlock(IList statements) { this.statements = statements; } bool IASTNode.Const() => false; object IASTNode.CreateFunction() { SSType type = ((IASTNode)this).Type(); if (type == SSType.none) { List> actions = new List>(); foreach (IASTNode statement in statements) { object func = statement.CreateFunction(); if (func is Action action) actions.Add(action); else { Func ofunc = (Func)func; actions.Add(ctx => ofunc.DynamicInvoke(ctx)); } } return (Action)(ctx => { foreach (Action action in actions) action(ctx); }); } List> actionsm1 = new List>(); foreach (IASTNode statement in statements) { if (actionsm1.Count == statements.Count - 1) break; object func = statement.CreateFunction(); if (func is Action action) actionsm1.Add(action); else { Delegate ofunc = (Delegate)func; actionsm1.Add(ctx => ofunc.DynamicInvoke(ctx)); } } if (type == SSType.real) { Func final = (Func)statements[statements.Count - 1].CreateFunction(); return (Func)(ctx => { foreach (Action action in actionsm1) action(ctx); return final(ctx); }); } if (type == SSType.boolean) { Func final = (Func)statements[statements.Count - 1].CreateFunction(); return (Func)(ctx => { foreach (Action action in actionsm1) action(ctx); return final(ctx); }); } throw new Exception("This should be unreachable"); } SSType IASTNode.Type() { return statements[statements.Count - 1].Type(); } } }