Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Forge Script
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Forge Script
Commits
598b1cb0
Verified
Commit
598b1cb0
authored
1 year ago
by
Louis
Browse files
Options
Downloads
Patches
Plain Diff
Impl AST conversions
parent
b777f752
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
forge-script-lang/src/parser/ast.rs
+25
-0
25 additions, 0 deletions
forge-script-lang/src/parser/ast.rs
forge-script-lang/src/parser/grammar.rs
+35
-35
35 additions, 35 deletions
forge-script-lang/src/parser/grammar.rs
with
60 additions
and
35 deletions
forge-script-lang/src/parser/ast.rs
+
25
−
0
View file @
598b1cb0
...
@@ -388,3 +388,28 @@ pub struct FunctionCall {
...
@@ -388,3 +388,28 @@ pub struct FunctionCall {
pub
name
:
Identifier
,
pub
name
:
Identifier
,
pub
params
:
ParameterList
,
pub
params
:
ParameterList
,
}
}
macro_rules!
impl_into_ast
{
(
$
(
$type
:
ty
=>
$variant
:
expr
),
+
)
=>
{
$
(
impl
From
<
$type
>
for
ValueExpression
{
fn
from
(
other
:
$type
)
->
Self
{
$variant
(
other
)
}
}
)
+
};
}
impl_into_ast!
(
GroupedExpression
=>
ValueExpression
::
Grouped
,
ExpressionList
=>
ValueExpression
::
Block
,
LiteralNode
=>
ValueExpression
::
Literal
,
DeclareIdent
=>
ValueExpression
::
DeclareIdentifier
,
Assignment
=>
ValueExpression
::
Assignment
,
Conditional
=>
ValueExpression
::
ConditionalBlock
,
Identifier
=>
ValueExpression
::
Identifier
,
FunctionCall
=>
ValueExpression
::
FunctionCall
,
DeclareFunction
=>
ValueExpression
::
DeclareFunction
,
TypeofValue
=>
ValueExpression
::
Typeof
);
This diff is collapsed.
Click to expand it.
forge-script-lang/src/parser/grammar.rs
+
35
−
35
View file @
598b1cb0
...
@@ -10,7 +10,7 @@ peg::parser! {
...
@@ -10,7 +10,7 @@ peg::parser! {
=
ex
:
expression_list
()
eof
()
{
Program
(
ex
)
}
=
ex
:
expression_list
()
eof
()
{
Program
(
ex
)
}
pub
rule
expression
()
->
Expression
pub
rule
expression
()
->
Expression
=
ex
:
precedenc
e_expression
()
{
Expression
::
Value
(
ex
)
}
=
ex
:
valu
e_expression
()
{
Expression
::
Value
(
ex
)
}
/
ex
:
void_expression
()
{
Expression
::
Void
(
ex
)
}
/
ex
:
void_expression
()
{
Expression
::
Void
(
ex
)
}
rule
void_expression
()
->
VoidExpression
rule
void_expression
()
->
VoidExpression
...
@@ -24,15 +24,15 @@ peg::parser! {
...
@@ -24,15 +24,15 @@ peg::parser! {
=
=
co
:
conditional_statement
()
{
ValueExpression
::
ConditionalBlock
(
co
)
}
co
:
conditional_statement
()
{
ValueExpression
::
ConditionalBlock
(
co
)
}
/
t
:
type_of
()
{
ValueExpression
::
Typeof
(
t
)
}
/
t
:
type_of
()
{
ValueExpression
::
Typeof
(
t
)
}
/
precedence
_expression
()
/
/ / binary
_expression()
/
decl
:
declare_variable
()
{
ValueExpression
::
DeclareIdentifier
(
decl
)
}
/
decl
:
declare_variable
()
{
ValueExpression
::
DeclareIdentifier
(
decl
)
}
/
decl
:
declare_function
()
{
ValueExpression
::
DeclareFunction
(
decl
)
}
/
decl
:
declare_function
()
{
ValueExpression
::
DeclareFunction
(
decl
)
}
/
name
:
simple_identifier
()
"("
params
:
param_list
()
?
")"
/
name
:
simple_identifier
()
"("
params
:
param_list
()
?
")"
{
ValueExpression
::
FunctionCall
(
FunctionCall
{
name
,
params
:
params
.unwrap_or_default
()
})
}
{
ValueExpression
::
FunctionCall
(
FunctionCall
{
name
,
params
:
params
.unwrap_or_default
()
})
}
// / left:value_expression() op:binary_operator() right:value_expression()
// { ValueExpression::Binary { lhs: Box::new(left), rhs: Box::new(right), operator: op } }
/
op
:
unary_operator
()
operand
:
value_expression
()
/
op
:
unary_operator
()
operand
:
value_expression
()
{
ValueExpression
::
Unary
{
operator
:
op
,
operand
:
Box
::
new
(
operand
)
}
}
{
ValueExpression
::
Unary
{
operator
:
op
,
operand
:
Box
::
new
(
operand
)
}
}
/
left
:
value_expression
()
op
:
binary_operator
()
right
:
value_expression
()
{
ValueExpression
::
Binary
{
lhs
:
Box
::
new
(
left
),
rhs
:
Box
::
new
(
right
),
operator
:
op
}
}
/
grouped
()
/
grouped
()
/
ident
:
simple_identifier
()
!
"("
{
ValueExpression
::
Identifier
(
ident
)
}
/
ident
:
simple_identifier
()
!
"("
{
ValueExpression
::
Identifier
(
ident
)
}
/
li
:
literal
()
{
ValueExpression
::
Literal
(
li
)
}
/
li
:
literal
()
{
ValueExpression
::
Literal
(
li
)
}
...
@@ -84,37 +84,37 @@ peg::parser! {
...
@@ -84,37 +84,37 @@ peg::parser! {
rule
expression_list
()
->
ExpressionList
rule
expression_list
()
->
ExpressionList
=
ex
:(
expression
()
**
";"
)
term
:
";"
?
{
ExpressionList
{
expressions
:
ex
,
is_void
:
term
.is_some
()
}
}
=
ex
:(
expression
()
**
";"
)
term
:
";"
?
{
ExpressionList
{
expressions
:
ex
,
is_void
:
term
.is_some
()
}
}
#[cache_left_rec]
//
#[cache_left_rec]
rule
precedence_expression
()
->
ValueExpression
//
rule precedence_expression() -> ValueExpression
=
precedence!
{
//
= precedence! {
"-"
z
:(
@
)
{
V
alue
E
xpression
::
Unary
{
operator
:
UnaryOp
::
Negate
,
operand
:
Box
::
new
(
z
)
}
}
// val:v
alue
_e
xpression
() { val
}
--
// "-" z:(@) { ValueExpression::Unary { operator: UnaryOp::Negate, operand: Box::new(z) } }
x
:(
@
)
"+"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Add
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
// --
x
:(
@
)
"
-
"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Subtract
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
//
x:(@) "
+
" y:@ { ValueExpression::Binary { operator: BinaryOp::
Add
, lhs: Box::new(x), rhs: Box::new(y) } }
--
// x:(@) "-" y:@ { ValueExpression::Binary { operator: BinaryOp::Subtract, lhs: Box::new(x), rhs: Box::new(y) } }
x
:(
@
)
"*"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Multiply
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
// --
x
:(
@
)
"
/
"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Divide
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
//
x:(@) "
*
" y:@ { ValueExpression::Binary { operator: BinaryOp::
Multiply
, lhs: Box::new(x), rhs: Box::new(y) } }
x
:(
@
)
"
%
"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Modulo
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
//
x:(@) "
/
" y:@ { ValueExpression::Binary { operator: BinaryOp::
Divide
, lhs: Box::new(x), rhs: Box::new(y) } }
--
// x:(@) "%" y:@ { ValueExpression::Binary { operator: BinaryOp::Modulo, lhs: Box::new(x), rhs: Box::new(y) } }
x
:(
@
)
"=="
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Equals
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
// --
x
:(
@
)
"
&&
"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
BoolAnd
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
//
x:(@) "
==
" y:@ { ValueExpression::Binary { operator: BinaryOp::
Equals
, lhs: Box::new(x), rhs: Box::new(y) } }
x
:(
@
)
"
||
"
y
:
@
{
ValueExpression
::
Binary
{
operator
:
BinaryOp
::
Bool
Or
,
lhs
:
Box
::
new
(
x
),
rhs
:
Box
::
new
(
y
)
}
}
//
x:(@) "
&&
" y:@ { ValueExpression::Binary { operator: BinaryOp::Bool
And
, lhs: Box::new(x), rhs: Box::new(y) } }
--
// x:(@) "||" y:@ { ValueExpression::Binary { operator: BinaryOp::BoolOr, lhs: Box::new(x), rhs: Box::new(y) } }
"!"
z
:(
@
)
{
ValueExpression
::
Unary
{
operator
:
UnaryOp
::
Not
,
operand
:
Box
::
new
(
z
)
}
}
// --
--
// "!" z:(@) { ValueExpression::Unary { operator: UnaryOp::Not, operand: Box::new(z) } }
"("
ex
:
precedence_expression
()
")"
{
ValueExpression
::
Grouped
(
GroupedExpression
{
inner
:
Box
::
new
(
ex
)
})
}
// --
val
:
valu
e_expression
()
{
v
al
}
// "(" ex:precedenc
e_expression()
")"
{
V
al
ueExpression::Grouped(GroupedExpression { inner: Box::new(ex) })
}
}
//
}
//
rule binary_operator() -> BinaryOp
rule
binary_operator
()
->
BinaryOp
//
= "+" { BinaryOp::Add }
=
"+"
{
BinaryOp
::
Add
}
//
/ "-" { BinaryOp::Subtract }
/
"-"
{
BinaryOp
::
Subtract
}
//
/ "*" { BinaryOp::Multiply }
/
"*"
{
BinaryOp
::
Multiply
}
//
/ "/" { BinaryOp::Divide }
/
"/"
{
BinaryOp
::
Divide
}
//
/ "%" { BinaryOp::Modulo }
/
"%"
{
BinaryOp
::
Modulo
}
//
/ "==" { BinaryOp::Equals }
/
"=="
{
BinaryOp
::
Equals
}
//
/ "&&" { BinaryOp::BoolAnd }
/
"&&"
{
BinaryOp
::
BoolAnd
}
//
/ "||" { BinaryOp::BoolOr }
/
"||"
{
BinaryOp
::
BoolOr
}
rule
unary_operator
()
->
UnaryOp
rule
unary_operator
()
->
UnaryOp
=
"!"
{
UnaryOp
::
Not
}
=
"!"
{
UnaryOp
::
Not
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment