#[derive(Debug, Clone)]
pub struct SourceLocation {
pub file: String,
pub line: usize,
pub column: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrimitiveTypeName {
Bigint,
Boolean,
ByteString,
PubKey,
Sig,
Sha256,
Ripemd160,
Addr,
SigHashPreimage,
RabinSig,
RabinPubKey,
Point,
Void,
}
impl PrimitiveTypeName {
pub fn from_str(s: &str) -> Option<PrimitiveTypeName> {
match s {
"bigint" => Some(PrimitiveTypeName::Bigint),
"boolean" => Some(PrimitiveTypeName::Boolean),
"ByteString" => Some(PrimitiveTypeName::ByteString),
"PubKey" => Some(PrimitiveTypeName::PubKey),
"Sig" => Some(PrimitiveTypeName::Sig),
"Sha256" => Some(PrimitiveTypeName::Sha256),
"Ripemd160" => Some(PrimitiveTypeName::Ripemd160),
"Addr" => Some(PrimitiveTypeName::Addr),
"SigHashPreimage" => Some(PrimitiveTypeName::SigHashPreimage),
"RabinSig" => Some(PrimitiveTypeName::RabinSig),
"RabinPubKey" => Some(PrimitiveTypeName::RabinPubKey),
"Point" => Some(PrimitiveTypeName::Point),
"void" => Some(PrimitiveTypeName::Void),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match self {
PrimitiveTypeName::Bigint => "bigint",
PrimitiveTypeName::Boolean => "boolean",
PrimitiveTypeName::ByteString => "ByteString",
PrimitiveTypeName::PubKey => "PubKey",
PrimitiveTypeName::Sig => "Sig",
PrimitiveTypeName::Sha256 => "Sha256",
PrimitiveTypeName::Ripemd160 => "Ripemd160",
PrimitiveTypeName::Addr => "Addr",
PrimitiveTypeName::SigHashPreimage => "SigHashPreimage",
PrimitiveTypeName::RabinSig => "RabinSig",
PrimitiveTypeName::RabinPubKey => "RabinPubKey",
PrimitiveTypeName::Point => "Point",
PrimitiveTypeName::Void => "void",
}
}
}
#[derive(Debug, Clone)]
pub enum TypeNode {
Primitive(PrimitiveTypeName),
FixedArray {
element: Box<TypeNode>,
length: usize,
},
Custom(String),
}
#[derive(Debug, Clone)]
pub struct ContractNode {
pub name: String,
pub parent_class: String, pub properties: Vec<PropertyNode>,
pub constructor: MethodNode,
pub methods: Vec<MethodNode>,
pub source_file: String,
}
#[derive(Debug, Clone)]
pub struct PropertyNode {
pub name: String,
pub prop_type: TypeNode,
pub readonly: bool,
pub initializer: Option<Expression>,
pub source_location: SourceLocation,
}
#[derive(Debug, Clone)]
pub struct MethodNode {
pub name: String,
pub params: Vec<ParamNode>,
pub body: Vec<Statement>,
pub visibility: Visibility,
pub source_location: SourceLocation,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Visibility {
Public,
Private,
}
#[derive(Debug, Clone)]
pub struct ParamNode {
pub name: String,
pub param_type: TypeNode,
}
#[derive(Debug, Clone)]
pub enum Statement {
VariableDecl {
name: String,
var_type: Option<TypeNode>,
mutable: bool,
init: Expression,
source_location: SourceLocation,
},
Assignment {
target: Expression,
value: Expression,
source_location: SourceLocation,
},
IfStatement {
condition: Expression,
then_branch: Vec<Statement>,
else_branch: Option<Vec<Statement>>,
source_location: SourceLocation,
},
ForStatement {
init: Box<Statement>, condition: Expression,
update: Box<Statement>,
body: Vec<Statement>,
source_location: SourceLocation,
},
ReturnStatement {
value: Option<Expression>,
source_location: SourceLocation,
},
ExpressionStatement {
expression: Expression,
source_location: SourceLocation,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinaryOp {
Add, Sub, Mul, Div, Mod, StrictEq, StrictNe, Lt, Le, Gt, Ge, And, Or, BitAnd, BitOr, BitXor, Shl, Shr, }
impl BinaryOp {
pub fn as_str(&self) -> &'static str {
match self {
BinaryOp::Add => "+",
BinaryOp::Sub => "-",
BinaryOp::Mul => "*",
BinaryOp::Div => "/",
BinaryOp::Mod => "%",
BinaryOp::StrictEq => "===",
BinaryOp::StrictNe => "!==",
BinaryOp::Lt => "<",
BinaryOp::Le => "<=",
BinaryOp::Gt => ">",
BinaryOp::Ge => ">=",
BinaryOp::And => "&&",
BinaryOp::Or => "||",
BinaryOp::BitAnd => "&",
BinaryOp::BitOr => "|",
BinaryOp::BitXor => "^",
BinaryOp::Shl => "<<",
BinaryOp::Shr => ">>",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnaryOp {
Not, Neg, BitNot, }
impl UnaryOp {
pub fn as_str(&self) -> &'static str {
match self {
UnaryOp::Not => "!",
UnaryOp::Neg => "-",
UnaryOp::BitNot => "~",
}
}
}
#[derive(Debug, Clone)]
pub enum Expression {
BinaryExpr {
op: BinaryOp,
left: Box<Expression>,
right: Box<Expression>,
},
UnaryExpr {
op: UnaryOp,
operand: Box<Expression>,
},
CallExpr {
callee: Box<Expression>,
args: Vec<Expression>,
},
MemberExpr {
object: Box<Expression>,
property: String,
},
Identifier {
name: String,
},
BigIntLiteral {
value: i64,
},
BoolLiteral {
value: bool,
},
ByteStringLiteral {
value: String, },
TernaryExpr {
condition: Box<Expression>,
consequent: Box<Expression>,
alternate: Box<Expression>,
},
PropertyAccess {
property: String, },
IndexAccess {
object: Box<Expression>,
index: Box<Expression>,
},
IncrementExpr {
operand: Box<Expression>,
prefix: bool,
},
DecrementExpr {
operand: Box<Expression>,
prefix: bool,
},
ArrayLiteral {
elements: Vec<Expression>,
},
}