[NotNull] private JToken SerializeStatement([NotNull] BaseStatement stmt) { switch (stmt) { case Goto g: return(new JObject { ["type"] = "statement::goto", ["expression"] = SerializeExpression(g.Destination) }); case If i: return(new JObject { ["type"] = "statement::if", ["condition"] = SerializeExpression(i.Condition), ["body"] = SerializeStatementList(i.TrueBranch), ["else_body"] = SerializeStatementList(i.FalseBranch) }); case Assignment a: return(SerializeAssignment(a)); case ExpressionWrapper e: return(new JObject { ["type"] = "statement::expression", ["expression"] = SerializeExpression(e.Expression) }); case StatementList _: throw new NotSupportedException(); default: throw new NotSupportedException($"Cannot serialize statement type `{stmt.GetType().Name}`"); } }
private static void Assert([NotNull] Model model, [NotNull] ITypeAssignments types, [NotNull] BaseStatement stmt) { switch (stmt) { default: throw new ArgumentOutOfRangeException(stmt.GetType().Name); case ExpressionWrapper _: case CompoundAssignment _: case If _: throw new NotSupportedException(stmt.GetType().Name); case EmptyStatement _: return; case Conditional conditional: Assert(model, conditional); return; case ErrorStatement errorStatement: Assert(model, errorStatement); return; case Assignment assignment: Assert(model, types, assignment); return; case Goto @goto: Assert(model, @goto); return; case StatementList statementList: foreach (var sub in statementList.Statements) { Assert(model, types, sub); } return; } }
private void _Reduce() { if (IsDebug) { Debug.Log(IceKori.PrettifyPrint(Statement.ToString())); } if (Statement.GetType() != typeof(DoNothing)) { var reduceValue = Statement.Reduce(Env, ErrorHandling); Statement = (BaseStatement)reduceValue[0]; Env = (Enviroment)reduceValue[1]; ErrorHandling = (ErrorHandling)reduceValue[2]; } else { State = InterpreterState.End; } }
public int PrepareStatement <TResult>(BaseStatement <TResult> statement) where TResult : BaseResult { int stmtId = Interlocked.Increment(ref _stmtId); switch (statement.GetType().Name) { case nameof(FindStatement): FindStatement fs = statement as FindStatement; Debug.Assert(fs != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Find, fs.Target.Schema.Name, fs.Target.Name, false, fs.FilterData, fs.findParams); break; case nameof(TableSelectStatement): TableSelectStatement ss = statement as TableSelectStatement; Debug.Assert(ss != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Find, ss.Target.Schema.Name, ss.Target.Name, true, ss.FilterData, ss.findParams); break; case nameof(ModifyStatement): ModifyStatement ms = statement as ModifyStatement; Debug.Assert(ms != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Update, ms.Target.Schema.Name, ms.Target.Name, false, ms.FilterData, null, ms.Updates); break; case nameof(TableUpdateStatement): TableUpdateStatement us = statement as TableUpdateStatement; Debug.Assert(us != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Update, us.Target.Schema.Name, us.Target.Name, true, us.FilterData, null, us.updates); break; case nameof(RemoveStatement): RemoveStatement rs = statement as RemoveStatement; Debug.Assert(rs != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Delete, rs.Target.Schema.Name, rs.Target.Name, false, rs.FilterData, null); break; case nameof(TableDeleteStatement): TableDeleteStatement ds = statement as TableDeleteStatement; Debug.Assert(ds != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Delete, ds.Target.Schema.Name, ds.Target.Name, true, ds.FilterData, null); break; case nameof(TableInsertStatement): TableInsertStatement insert = statement as TableInsertStatement; Debug.Assert(insert != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Insert, insert.Target.Schema.Name, insert.Target.Name, true, null, null, null, insert.values.ToArray(), insert.fields, false); break; case nameof(SqlStatement): SqlStatement sqlStatement = statement as SqlStatement; Debug.Assert(sqlStatement != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.SqlStatement, null, null, true, null, null, null, sqlStatement.parameters.ToArray(), null, false, sqlStatement.SQL); break; default: throw new NotSupportedException(statement.GetType().Name); } _preparedStatements.Add(stmtId); return(stmtId); }
[NotNull] protected virtual TResult VisitUnknown(BaseStatement statement) { throw new InvalidOperationException($"`Visit` invalid for statement type `{statement.GetType().FullName}`"); }
private JToken SerializeStatement(BaseStatement stmt) { return(stmt switch { Goto g => new JObject { ["type"] = "statement::goto", ["expression"] = SerializeExpression(g.Destination) }, If i => new JObject { ["type"] = "statement::if", ["condition"] = SerializeExpression(i.Condition), ["body"] = SerializeStatementList(i.TrueBranch), ["else_body"] = SerializeStatementList(i.FalseBranch) }, Assignment a => SerializeAssignment(a), ExpressionWrapper e => new JObject { ["type"] = "statement::expression", ["expression"] = SerializeExpression(e.Expression) }, StatementList _ => throw new NotSupportedException(), _ => throw new NotSupportedException($"Cannot serialize statement type `{stmt.GetType().Name}`") });