Merge pull request #62 from mist-go/syntax-changes

Syntax changes
This commit is contained in:
2026-06-03 20:01:16 +02:00
committed by GitHub
7 changed files with 97 additions and 53 deletions
+13 -30
View File
@@ -38,7 +38,11 @@ impl GenRust for Literal {
Self::Tuple(values) => { Self::Tuple(values) => {
cg.add("("); cg.add("(");
for val in values { for (i, val) in values.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
val.gen_rust(ctx, cg); val.gen_rust(ctx, cg);
} }
@@ -87,7 +91,6 @@ impl GenRust for Expression {
} => { } => {
prefixes.gen_rust(ctx, cg); prefixes.gen_rust(ctx, cg);
initial.gen_rust(ctx, cg); initial.gen_rust(ctx, cg);
cg.add(&Some(prefixes).get_rust());
for postfix in postfixes { for postfix in postfixes {
postfix.gen_rust(ctx, cg); postfix.gen_rust(ctx, cg);
} }
@@ -117,7 +120,6 @@ impl GenRust for Prefix {
Self::Ref => cg.add("&"), Self::Ref => cg.add("&"),
Self::RefMut => cg.add("&mut "), Self::RefMut => cg.add("&mut "),
Self::Not => cg.add("!"), Self::Not => cg.add("!"),
Self::New(_) => cg.add(""),
Self::Neg => cg.add("-"), Self::Neg => cg.add("-"),
Self::Closure(ty, args) => { Self::Closure(ty, args) => {
cg.add("|"); cg.add("|");
@@ -148,30 +150,6 @@ impl GenRust for Vec<Prefix> {
} }
} }
impl GetRust for Option<&Vec<Prefix>> {
fn get_rust(&self) -> String {
if let Some(prefixes) = self {
prefixes
.into_iter()
.last()
.map(|p| match p {
Prefix::New(generics) => format!(
"::new{}",
generics
.clone()
.map(|v| format!("::{}", v.get_rust()))
.unwrap_or_default(),
),
_ => String::new(),
})
.unwrap_or_default()
.to_string()
} else {
String::new()
}
}
}
impl GenRust for Postfix { impl GenRust for Postfix {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
@@ -198,10 +176,15 @@ impl GenRust for Postfix {
cg.add(")"); cg.add(")");
} }
Postfix::MacroCall(inner) => { Postfix::MacroCall { inner, delimiter } => {
cg.add("!("); let (open, close) = match delimiter {
MacroDelimiter::Paren => ("!(", ")"),
MacroDelimiter::Bracket => ("![", "]"),
MacroDelimiter::Brace => ("!{", "}"),
};
cg.add(open);
cg.add(inner); cg.add(inner);
cg.add(")"); cg.add(close);
} }
Postfix::StructCall(fields) => { Postfix::StructCall(fields) => {
+29 -2
View File
@@ -196,12 +196,21 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
cg.add_indented(&format!("this.constructor(")); cg.add_indented(&format!("this.constructor("));
for (i, param) in self.1.item.params.0.iter().enumerate() { for (i, param) in self
.1
.item
.params
.0
.iter()
.map(|v| expr_pattern(&v.name))
.enumerate()
{
if i > 0 { if i > 0 {
cg.add(", "); cg.add(", ");
} }
param.name.gen_rust(ctx, cg); ctx.expr_ensure_semicolon = false;
param.gen_rust(ctx, cg);
} }
cg.addln(");"); cg.addln(");");
@@ -237,6 +246,24 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
} }
} }
pub fn expr_pattern(pat: &Pattern) -> Expression {
match pat {
Pattern::Literal(v) => Expression::Literal(v.clone()),
Pattern::Path(_, v) => Expression::Path(v.clone().into()),
Pattern::Tuple(items) => Expression::Literal(Literal::Tuple(
items.iter().map(|v| expr_pattern(&v)).collect(),
)),
Pattern::NamedTuple(path, items) => Expression::Fix {
initial: Box::new(Expression::Path(path.clone().into())),
prefixes: Vec::new(),
postfixes: vec![Postfix::Call(
items.iter().map(|v| expr_pattern(&v)).collect(),
)],
},
Pattern::Struct(_, _) => unimplemented!(),
}
}
impl GenRust for TopLevelKind { impl GenRust for TopLevelKind {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
+25 -2
View File
@@ -39,11 +39,21 @@ pub enum Pattern {
Path(bool, Path), Path(bool, Path),
} }
#[derive(Debug, Clone, Serialize)]
pub enum MacroDelimiter {
Paren,
Bracket,
Brace,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Postfix { pub enum Postfix {
FieldAccess(Identifier, Option<Generics>), FieldAccess(Identifier, Option<Generics>),
Call(Vec<Expression>), Call(Vec<Expression>),
MacroCall(String), MacroCall {
inner: String,
delimiter: MacroDelimiter,
},
StructCall(Vec<(Identifier, Expression)>), StructCall(Vec<(Identifier, Expression)>),
Assign(String, Box<Expression>), Assign(String, Box<Expression>),
Index(Expression), Index(Expression),
@@ -58,7 +68,6 @@ pub enum Prefix {
Ref, Ref,
RefMut, RefMut,
Deref, Deref,
New(Option<Generics>),
Not, Not,
Neg, Neg,
Closure(Option<TypeExpr>, Vec<VarDecl>), Closure(Option<TypeExpr>, Vec<VarDecl>),
@@ -91,3 +100,17 @@ impl Expression {
} }
} }
} }
impl From<Path> for ExprPath {
fn from(path: Path) -> Self {
Self(
path.0
.into_iter()
.map(|v| ExprPathSegment {
ident: v,
generics: None,
})
.collect(),
)
}
}
+12 -10
View File
@@ -25,7 +25,7 @@ keyword = {
| "impl" | "impl"
| "pub" | "pub"
| "mut" | "mut"
| "var" | "let"
| "true" | "true"
| "false" | "false"
| "dyn" | "dyn"
@@ -75,7 +75,7 @@ program = {
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
mutable = { "mut" } mutable = { "mut" }
var = { "var" } let_kw = { "let" }
// ====================================================== // ======================================================
// GENERICS // GENERICS
@@ -190,7 +190,7 @@ attributes = {
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
var_decl = { var_decl = {
(var | type_expr) ~ pattern (let_kw | type_expr) ~ pattern
} }
field = { field = {
@@ -475,7 +475,9 @@ array = {
macro_inner_v = @{ macro_inner_v = @{
string_lit string_lit
| ("(" ~ macro_inner_v* ~ ")") | ("(" ~ macro_inner_v* ~ ")")
| (!("(" | ")") ~ ANY) | ("[" ~ macro_inner_v* ~ "]")
| ("{" ~ macro_inner_v* ~ "}")
| (!("(" | ")" | "[" | "]" | "{" | "}") ~ ANY)
} }
// ------------------------------------------------------ // ------------------------------------------------------
@@ -511,9 +513,9 @@ call_px = {
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")" "(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
} }
macro_call_px = { macro_call_paren = { "!" ~ "(" ~ macro_inner_v* ~ ")" }
"!" ~ "(" ~ macro_inner_v* ~ ")" macro_call_bracket = { "!" ~ "[" ~ macro_inner_v* ~ "]" }
} macro_call_brace = { "!" ~ "{" ~ macro_inner_v* ~ "}" }
struct_field_decl = { struct_field_decl = {
identifier ~ ":" ~ expr identifier ~ ":" ~ expr
@@ -546,7 +548,9 @@ decrement = {
postfix = { postfix = {
field_px field_px
| call_px | call_px
| macro_call_px | macro_call_paren
| macro_call_bracket
| macro_call_brace
| struct_px | struct_px
| index_px | index_px
| as_px | as_px
@@ -562,7 +566,6 @@ postfix = {
deref_px = { "*" } deref_px = { "*" }
mut_ref_px = { "&" ~ mutable } mut_ref_px = { "&" ~ mutable }
ref_px = { "&" } ref_px = { "&" }
new_px = { "new" ~ generics? }
not_px = { "!" } not_px = { "!" }
neg_px = { "-" } neg_px = { "-" }
@@ -571,7 +574,6 @@ prefix = {
| deref_px | deref_px
| mut_ref_px | mut_ref_px
| ref_px | ref_px
| new_px
| not_px | not_px
| neg_px | neg_px
} }
+1 -1
View File
@@ -54,7 +54,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
let type_ = inner let type_ = inner
.next() .next()
.and_then(|pair| { .and_then(|pair| {
if pair.as_str().trim() == "var" { if pair.as_rule() == Rule::let_kw {
None None
} else { } else {
Some(TypeExpr::try_from(pair)) Some(TypeExpr::try_from(pair))
+12 -7
View File
@@ -106,12 +106,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
Rule::deref_px => Self::Deref, Rule::deref_px => Self::Deref,
Rule::mut_ref_px => Self::RefMut, Rule::mut_ref_px => Self::RefMut,
Rule::ref_px => Self::Ref, Rule::ref_px => Self::Ref,
Rule::new_px => Self::New(
pair.into_inner()
.next()
.map(|v| v.try_into().get())
.transpose()?,
),
Rule::not_px => Self::Not, Rule::not_px => Self::Not,
Rule::neg_px => Self::Neg, Rule::neg_px => Self::Neg,
Rule::closure => { Rule::closure => {
@@ -161,7 +155,18 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
ast_expr!(Postfix::Index(Expression::try_from(inner.next().unwrap()))) ast_expr!(Postfix::Index(Expression::try_from(inner.next().unwrap())))
} }
Rule::macro_call_px => Ok(Postfix::MacroCall(inner.as_str().to_string())), Rule::macro_call_paren => Ok(Postfix::MacroCall {
inner: inner.as_str().to_string(),
delimiter: MacroDelimiter::Paren,
}),
Rule::macro_call_bracket => Ok(Postfix::MacroCall {
inner: inner.as_str().to_string(),
delimiter: MacroDelimiter::Bracket,
}),
Rule::macro_call_brace => Ok(Postfix::MacroCall {
inner: inner.as_str().to_string(),
delimiter: MacroDelimiter::Brace,
}),
Rule::as_px => { Rule::as_px => {
ast_expr!(Postfix::As(inner.next().unwrap().try_into())) ast_expr!(Postfix::As(inner.next().unwrap().try_into()))
+5 -1
View File
@@ -1,3 +1,7 @@
class Bro {
constructor([str*, str*] [idk, bro]) {}
}
void main() { void main() {
test_package::hello(); test_package::hello();
} }