public static string Format(Unit unit) { if (unit.HasUnit) return "[" + unit + "]"; else return ""; }
public static Unit operator *(Unit a, Unit b) { var c = new Unit(); foreach (var e in a.Exponent) c.AddExponent(e.Key, e.Value); foreach (var e in b.Exponent) c.AddExponent(e.Key, e.Value); return c; }
public static Unit operator /(Unit a, Unit b) { var c = new Unit(); foreach (var e in a.Exponent) c.AddExponent(e.Key, e.Value); //Above the division foreach (var e in b.Exponent) c.AddExponent(e.Key, -e.Value);//Below the division return c; }
public LaxType(DataType type, Unit unit) { Type = type; Unit = unit; }
static Unit GetUnit(Unit left, BinaryOp op, Unit right) { switch (op.Operator) { case "+": case "-": case "and": case "or": if (left != right) throw new SemanticError(op, "Unit mismatch: " + left + op.Operator + right); return left; case "*": return left * right; case "/": return left / right; default: throw new NotImplementedException(op.Operator); } }