#[derive(Debug, Clone)]
pub struct Program {
pub package: String,
pub imports: Vec<String>,
pub types: Vec<StructDecl>,
pub interfaces: Vec<InterfaceDecl>,
pub main: Vec<Stmt>,
pub funcs: Vec<Func>,
pub defined: Vec<(String, String)>,
}
#[derive(Debug, Clone)]
pub struct StructDecl {
pub name: String,
pub fields: Vec<Param>,
}
#[derive(Debug, Clone)]
pub struct InterfaceDecl {
pub name: String,
pub methods: Vec<String>,
}
pub fn method_sig(name: &str, arity: usize, results: &[String]) -> String {
format!("{name}/{arity}:{}", results.join(","))
}
#[derive(Debug, Clone)]
pub struct Func {
pub name: String,
pub receiver: Option<Param>,
pub params: Vec<Param>,
pub variadic: bool,
pub results: Vec<String>,
pub result_names: Vec<String>,
pub body: Vec<Stmt>,
pub line: u32,
}
#[derive(Debug, Clone)]
pub struct Param {
pub name: String,
pub ty: String,
}
#[derive(Debug, Clone)]
pub enum Stmt {
Var {
name: String,
ty: Option<String>,
init: Option<Expr>,
line: u32,
},
Short {
names: Vec<String>,
values: Vec<Expr>,
line: u32,
},
Assign {
target: Expr,
op: AssignOp,
value: Expr,
line: u32,
},
AssignMulti {
targets: Vec<Expr>,
values: Vec<Expr>,
line: u32,
},
IncDec { target: Expr, inc: bool, line: u32 },
ExprStmt(Expr),
Return(Vec<Expr>, u32),
If {
init: Option<Box<Stmt>>,
cond: Expr,
then: Vec<Stmt>,
els: Vec<Stmt>,
line: u32,
},
For {
label: Option<String>,
init: Option<Box<Stmt>>,
cond: Option<Expr>,
post: Option<Box<Stmt>>,
body: Vec<Stmt>,
line: u32,
},
ForRange {
label: Option<String>,
key: Option<String>,
val: Option<String>,
define: bool,
iter: Expr,
body: Vec<Stmt>,
line: u32,
},
Go { call: Expr, line: u32 },
Defer { call: Expr, line: u32 },
Send { chan: Expr, val: Expr, line: u32 },
Select {
cases: Vec<SelectClause>,
default: Option<Vec<Stmt>>,
line: u32,
},
Switch {
label: Option<String>,
init: Option<Box<Stmt>>,
tag: Option<Expr>,
cases: Vec<SwitchCase>,
default: Option<Vec<Stmt>>,
line: u32,
},
TypeSwitch {
init: Option<Box<Stmt>>,
bind: Option<String>,
expr: Expr,
cases: Vec<TypeSwitchCase>,
default: Option<Vec<Stmt>>,
line: u32,
},
Fallthrough(u32),
Break(u32, Option<String>),
Continue(u32, Option<String>),
Block(Vec<Stmt>),
}
#[derive(Debug, Clone)]
pub struct SwitchCase {
pub exprs: Vec<Expr>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone)]
pub struct TypeSwitchCase {
pub types: Vec<String>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone)]
pub struct SelectClause {
pub comm: SelectComm,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone)]
pub enum SelectComm {
Recv {
bind: Option<String>,
ok_bind: Option<String>,
chan: Expr,
},
Send { chan: Expr, val: Expr },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssignOp {
Set,
Add,
Sub,
Mul,
Div,
Mod,
BitAnd,
BitOr,
BitXor,
Shl,
Shr,
AndNot,
}
#[derive(Debug, Clone)]
pub enum Expr {
Int(i64),
Float(f64, Option<(i128, i32)>),
Str(String),
Bool(bool),
Ident(String),
Unary {
op: UnOp,
rhs: Box<Expr>,
},
Binary {
op: BinOp,
lhs: Box<Expr>,
rhs: Box<Expr>,
},
Call {
func: Box<Expr>,
args: Vec<Expr>,
spread: bool,
line: u32,
},
Selector {
recv: Box<Expr>,
field: String,
},
Index {
recv: Box<Expr>,
index: Box<Expr>,
},
Slice {
recv: Box<Expr>,
low: Option<Box<Expr>>,
high: Option<Box<Expr>>,
max: Option<Box<Expr>>,
},
SliceLit {
elem_ty: String,
elems: Vec<Expr>,
array_len: Option<usize>,
},
MapLit {
key_ty: String,
val_ty: String,
pairs: Vec<(Expr, Expr)>,
},
StructLit {
type_name: String,
fields: Vec<(Option<String>, Expr)>,
},
Make {
is_map: bool,
elem_ty: String,
len: Option<Box<Expr>>,
cap: Option<Box<Expr>>,
elem_zero: Box<Expr>,
},
MakeChan {
cap: Option<Box<Expr>>,
elem_ty: String,
},
Recv {
chan: Box<Expr>,
},
FuncLit {
params: Vec<Param>,
results: Vec<String>,
body: Vec<Stmt>,
},
TypeAssert {
expr: Box<Expr>,
ty: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnOp {
Neg,
Not,
BitNot,
Addr,
Deref,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
And,
Or,
BitAnd,
BitOr,
BitXor,
Shl,
Shr,
AndNot,
}
pub fn set_stmt_label(s: &mut Stmt, name: &str) -> bool {
match s {
Stmt::For { label, .. } | Stmt::ForRange { label, .. } | Stmt::Switch { label, .. } => {
*label = Some(name.to_string());
true
}
_ => false,
}
}
pub fn array_elem_ty(ty: &str) -> Option<&str> {
let rest = ty.strip_prefix('[')?;
let close = rest.find(']')?;
if close == 0 {
return None; }
Some(&rest[close + 1..])
}
pub fn array_len_of(ty: &str) -> Option<usize> {
let rest = ty.strip_prefix('[')?;
let close = rest.find(']')?;
rest[..close].parse().ok()
}