Amogulator/Suslang/AST.cs
2023-03-14 12:53:56 +01:00

86 lines
1.9 KiB
C#

using System;
namespace Amogulator.Suslang;
public interface Expr {
string astString(int depth);
}
public class ErrorExpr : Expr {
public Position position;
public string message;
public string astString(int depth) =>
$"ErrorExpr {{ {this.position.line}:{this.position.col} \"{this.message}\" }}";
}
public class IdExpr : Expr {
public string value;
public string astString(int depth) =>
$"IdExpr {{ \"{this.value}\" }}";
}
public class IntExpr : Expr {
public int value;
public string astString(int depth) =>
$"IntExpr {{ {this.value} }}";
}
public class FloatExpr : Expr {
public double value;
public string astString(int depth) =>
$"FloatExpr {{ {this.value} }}";
}
public class StringExpr : Expr {
public string value;
public string astString(int depth) =>
$"FloatExpr {{ \"{this.value}\" }}";
}
public class BoolExpr : Expr {
public bool value;
public string astString(int depth) =>
$"BoolExpr {{ {this.value} }}";
}
public enum UnaryType {
Not,
Negate,
}
public class UnaryExpr : Expr {
public UnaryType type;
public Expr value;
public string astString(int depth) =>
$"UnaryExpr {{\n{new string(' ', (depth + 1) * 4)}type: "
+ $"{this.type},\n{new string(' ', (depth + 1) * 4)}value: "
+ $"{this.value.astString(depth + 1)}\n{new String(' ', depth * 4)}}}";
}
public enum BinaryType {
Add,
Subtract,
Multiply,
Divide,
Modulo,
}
public class BinaryExpr : Expr {
public BinaryType type;
public Expr left, right;
public string astString(int depth) =>
$"BinaryExpr {{\n{new String(' ', (depth + 1) * 4)}type: "
+ $"{this.type.ToString()},\n{new String(' ', (depth + 1) * 4)}left: "
+ $"{this.left.astString(depth + 1)},\n{new String(' ', (depth + 1) * 4)}right: "
+ $"{this.right.astString(depth + 1)},\n{new String(' ', depth * 4)}}}";
}