public override AST.Node ToAST(Env env) { /// Check the switch statement. Switch sw = env.GetSwitch(); if (sw == null) { throw new Error(Pos, "default statement not in switch statement"); } /// The expression of a case should be constant integer expression. AST.ConstIntegerExpr c = expr.ToASTExpr(env) as AST.ConstIntegerExpr; if (c == null) { throw new Error(Pos, "the expression of a case should be constant integer expression"); } /// No two of the case constant expressions shall have the same value. /// TODO: The conversion. foreach (var e in sw.cases) { if (c.value == e.Item2.value) { throw new Error(Pos, string.Format("duplicate value {0} in case", c.value)); } } string label = env.AllocCaseLabel(); sw.cases.AddLast(new Tuple <string, ConstIntegerExpr>(label, c)); AST.Node s = stmt.ToAST(env); return(new AST.Labeled(label, s)); }
public override AST.Node ToAST(Env env) { /// Check the switch statement. Switch sw = env.GetSwitch(); if (sw == null) { throw new Error(Pos, "default statement not in switch statement"); } /// At most one default statement in a switch. if (sw.defaultLabel != null) { throw new Error(Pos, "at most one default label in a switch statement"); } string label = env.AllocDefaultLabel(); sw.defaultLabel = label; AST.Node s = stmt.ToAST(env); return(new AST.Labeled(label, s)); }