Skip to content

Commit

Permalink
feat: simple floating point numbers
Browse files Browse the repository at this point in the history
Co-authored-by: ishwar00 <[email protected]>
  • Loading branch information
Samyak2 and ishwar00 committed Nov 4, 2023
1 parent bc0647e commit 18523c5
Show file tree
Hide file tree
Showing 5 changed files with 627 additions and 3 deletions.
14 changes: 12 additions & 2 deletions compiler/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ pub enum Expr<'i> {
span: Span<'i>,
},
Integer {
value: i32,
value: i64, // TODO: other int types?
span: Span<'i>,
},
Float {
value: f64, // TODO: other float types?
span: Span<'i>,
},
FunctionCall {
Expand Down Expand Up @@ -390,6 +394,10 @@ impl<'i> Node<'i> for Expr<'i> {
value: pair.as_str().parse().unwrap(),
span: pair.as_span(),
}),
Rule::FloatLit => Ok(Expr::Float {
value: pair.as_str().parse().unwrap(),
span: pair.as_span(),
}),
Rule::FunctionCall => Ok(Self::parse_expr(pair.into_inner())?),
invalid_rule => Err(BakugoParsingError::new(
pair.as_span(),
Expand Down Expand Up @@ -437,7 +445,9 @@ impl<'i> Expr<'i> {
PRATT_PARSER
.map_primary(|primary| match primary.as_rule() {
// TODO: check if this match is needed
Rule::IntLit | Rule::Ident | Rule::FunctionCall => Expr::parse(primary),
Rule::FloatLit | Rule::IntLit | Rule::Ident | Rule::FunctionCall => {
Expr::parse(primary)
}
invalid_rule => Err(BakugoParsingError::new(
primary.as_span(),
format!("expected an expresion. got {:?}", invalid_rule),
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/bakugo.pest
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Comma = { "," }
Ident = @{ Letter ~ (Letter | UnicodeDigit)* }

IntLit = @{ "0" | ('1'..'9' ~ ("_"? ~ DecimalDigits)?) }
FloatLit = @{ DecimalDigits ~ "." ~ DecimalDigits? | "." ~ DecimalDigits }
DecimalDigits = @{ DecimalDigit ~ ("_"? ~ DecimalDigit)* }

/// Rune literals
Expand Down Expand Up @@ -120,7 +121,7 @@ FunctionBody = _{ Block }
// Operands
Operand = _{ Literal | OperandName | "(" ~ Expression ~ ")" }
Literal = _{ BasicLit }
BasicLit = _{ IntLit | RuneLit | StringLit }
BasicLit = _{ FloatLit | IntLit | RuneLit | StringLit }
OperandName = _{ Ident }

// Primary expressions
Expand Down
8 changes: 8 additions & 0 deletions compiler/tests/examples/09_floats.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func main() {
var a = 10.0;
var b = 15.;
const c = .15;
const d = 05.;
const e = .001;
const f = 0001.1000;
};
170 changes: 170 additions & 0 deletions compiler/tests/snapshots/parser__ast_from_path@09_floats.bakugo.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
---
source: tests/parser.rs
expression: parsed
input_file: tests/examples/09_floats.bakugo
---
SourceFile {
top_level: [
FnDecl(
FnDecl {
name: Ident {
value: "main",
span: Span {
str: "main",
start: 5,
end: 9,
},
},
params: [],
result_kind: None,
body: StatementList(
[
Declaration(
Var(
VarConstDecl {
name: Ident {
value: "a",
span: Span {
str: "a",
start: 22,
end: 23,
},
},
kind: None,
expr: Float {
value: 10.0,
span: Span {
str: "10.0",
start: 26,
end: 30,
},
},
},
),
),
Declaration(
Var(
VarConstDecl {
name: Ident {
value: "b",
span: Span {
str: "b",
start: 40,
end: 41,
},
},
kind: None,
expr: Float {
value: 15.0,
span: Span {
str: "15.",
start: 44,
end: 47,
},
},
},
),
),
Declaration(
Const(
VarConstDecl {
name: Ident {
value: "c",
span: Span {
str: "c",
start: 59,
end: 60,
},
},
kind: None,
expr: Float {
value: 0.15,
span: Span {
str: ".15",
start: 63,
end: 66,
},
},
},
),
),
Declaration(
Const(
VarConstDecl {
name: Ident {
value: "d",
span: Span {
str: "d",
start: 78,
end: 79,
},
},
kind: None,
expr: Float {
value: 5.0,
span: Span {
str: "05.",
start: 82,
end: 85,
},
},
},
),
),
Declaration(
Const(
VarConstDecl {
name: Ident {
value: "e",
span: Span {
str: "e",
start: 97,
end: 98,
},
},
kind: None,
expr: Float {
value: 0.001,
span: Span {
str: ".001",
start: 101,
end: 105,
},
},
},
),
),
Declaration(
Const(
VarConstDecl {
name: Ident {
value: "f",
span: Span {
str: "f",
start: 117,
end: 118,
},
},
kind: None,
expr: Float {
value: 1.1,
span: Span {
str: "0001.1000",
start: 121,
end: 130,
},
},
},
),
),
],
),
span: Span {
str: "func main() {\n var a = 10.0;\n var b = 15.;\n const c = .15;\n const d = 05.;\n const e = .001;\n const f = 0001.1000;\n}",
start: 0,
end: 133,
},
},
),
],
}
Loading

0 comments on commit 18523c5

Please sign in to comment.