normalize_surface_syntax/ir/
mod.rs1mod expr;
10mod pat;
11mod stmt;
12mod structure_eq;
13
14pub use expr::*;
15pub use pat::*;
16pub use stmt::*;
17pub use structure_eq::StructureEq;
18
19use serde::{Deserialize, Serialize};
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27pub struct Span {
28 pub start_line: u32,
29 pub start_col: u32,
30 pub end_line: u32,
31 pub end_col: u32,
32}
33
34impl Span {
35 pub fn from_ts(start: tree_sitter::Point, end: tree_sitter::Point) -> Self {
40 Self {
41 start_line: start.row as u32 + 1,
42 start_col: start.column as u32,
43 end_line: end.row as u32 + 1,
44 end_col: end.column as u32,
45 }
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub struct Program {
52 pub body: Vec<Stmt>,
54}
55
56impl Program {
57 pub fn new(body: Vec<Stmt>) -> Self {
58 Self { body }
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct Param {
65 pub name: String,
67 #[serde(skip_serializing_if = "Option::is_none")]
70 pub type_annotation: Option<String>,
71}
72
73impl Param {
74 pub fn new(name: impl Into<String>) -> Self {
76 Self {
77 name: name.into(),
78 type_annotation: None,
79 }
80 }
81
82 pub fn typed(name: impl Into<String>, annotation: impl Into<String>) -> Self {
84 Self {
85 name: name.into(),
86 type_annotation: Some(annotation.into()),
87 }
88 }
89}
90
91impl From<&str> for Param {
92 fn from(s: &str) -> Self {
93 Param::new(s)
94 }
95}
96
97impl From<String> for Param {
98 fn from(s: String) -> Self {
99 Param::new(s)
100 }
101}
102
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
105pub struct Function {
106 pub name: String,
108 pub params: Vec<Param>,
110 #[serde(skip_serializing_if = "Option::is_none")]
113 pub return_type: Option<String>,
114 pub body: Vec<Stmt>,
116}
117
118impl Function {
119 pub fn new(name: impl Into<String>, params: Vec<Param>, body: Vec<Stmt>) -> Self {
120 Self {
121 name: name.into(),
122 params,
123 return_type: None,
124 body,
125 }
126 }
127
128 pub fn anonymous(params: Vec<Param>, body: Vec<Stmt>) -> Self {
129 Self::new("", params, body)
130 }
131}