Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
yuca
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Valencia Vargas, Carlos
yuca
Commits
edc52ccc
Commit
edc52ccc
authored
3 years ago
by
Carlos Valencia
Browse files
Options
Downloads
Patches
Plain Diff
Better Expr AST
parent
8f3fc620
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/examples/example_programs/arithmetic/expression.rs
+60
-30
60 additions, 30 deletions
src/examples/example_programs/arithmetic/expression.rs
src/main.rs
+9
-7
9 additions, 7 deletions
src/main.rs
with
69 additions
and
37 deletions
src/examples/example_programs/arithmetic/expression.rs
+
60
−
30
View file @
edc52ccc
use
std
::
fmt
::
Debug
;
use
crate
::
Parser
;
use
crate
::
parser
::
Context
;
...
...
@@ -10,48 +12,76 @@ use crate::parser_impls::whitespace::{Whitespace};
// This version of Expression places the resursive part in ()
pub
(
crate
)
struct
Expr
;
impl
Parser
<
(
char
,
(
String
,
String
))
>
for
Expr
{
fn
parse_fun
(
&
self
,
input
:
&
str
,
_ctx
:
&
mut
Context
)
->
Result
<
((
char
,
(
String
,
String
)),
String
),
String
>
{
pub
struct
ExprOutput
{
left
:
String
,
operator
:
char
,
right
:
Box
<
Rest
>
,
}
#[derive(Debug)]
enum
Rest
{
I
(
String
),
E
(
ExprOutput
)
}
let
parser_plus
=
Map
{
map_a_b_fun
:
|(
plus
,
w
)
:
(
char
,
char
)|
{
plus
},
parser_a
:
Box
::
new
(
Zip
{
parser_first
:
Box
::
new
(
Alternative
{
parser_first
:
Box
::
new
(
Sat
{
sat_fun
:
|
c
|
{
c
==
'+'
}
}),
parser_second
:
Box
::
new
(
Sat
{
sat_fun
:
|
c
|
{
c
==
'-'
}
}),
impl
Debug
for
ExprOutput
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
write!
(
f
,
"{} {} {:#?}"
,
self
.left
,
self
.operator
,
self
.right
)
}
}
impl
Parser
<
ExprOutput
>
for
Expr
{
fn
parse_fun
(
&
self
,
input
:
&
str
,
_ctx
:
&
mut
Context
)
->
Result
<
(
ExprOutput
,
String
),
String
>
{
let
parse_left
=
Zip
{
parser_first
:
Box
::
new
(
Map
{
map_a_b_fun
:
|(
num
,
_w
)
:
(
String
,
char
)|
num
,
parser_a
:
Box
::
new
(
Zip
{
parser_first
:
Box
::
new
(
Int
),
parser_second
:
Box
::
new
(
Whitespace
)
}),
parser_second
:
Box
::
new
(
Whitespace
)
}),
parser_second
:
Box
::
new
(
Map
{
map_a_b_fun
:
|(
c
,
w
):
(
char
,
char
)|
c
,
parser_a
:
Box
::
new
(
Zip
{
parser_first
:
Box
::
new
(
Alternative
{
parser_first
:
Box
::
new
(
Sat
{
sat_fun
:
|
c
|
c
==
'+'
}),
parser_second
:
Box
::
new
(
Sat
{
sat_fun
:
|
c
|
c
==
'-'
})
}),
parser_second
:
Box
::
new
(
Whitespace
)
})
})
};
let
parser_second_int
=
Map
{
map_a_b_fun
:
|(
_w
,
c
)
:
(
char
,
String
)|
{
c
},
Map
{
map_a_b_fun
:
|((
num
,
c
),
rest
):
((
String
,
char
),
Rest
)|
{
ExprOutput
{
left
:
num
,
operator
:
c
,
right
:
Box
::
new
(
rest
)
}
},
parser_a
:
Box
::
new
(
Zip
{
parser_first
:
Box
::
new
(
Whitespace
),
parser_first
:
Box
::
new
(
parse_left
),
parser_second
:
Box
::
new
(
Alternative
{
parser_first
:
Box
::
new
(
Map
{
map_a_b_fun
:
|
(
c
,
(
sa
,
sb
)):
(
char
,
(
String
,
String
))|
{
format!
(
"({} {} {})"
,
c
,
sa
,
sb
)}
,
map_a_b_fun
:
|
eo
|
Rest
::
E
(
eo
)
,
parser_a
:
Box
::
new
(
Expr
)
}),
parser_second
:
Box
::
new
(
Int
)
})
})
};
let
parser_ints
=
Zip
{
parser_first
:
Box
::
new
(
Int
),
parser_second
:
Box
::
new
(
parser_second_int
)
};
parser_second
:
Box
::
new
(
Map
{
map_a_b_fun
:
|
eo
|
Rest
::
I
(
eo
),
parser_a
:
Box
::
new
(
Int
)
})
})
Zip
{
parser_first
:
Box
::
new
(
parser_plus
),
parser_second
:
Box
::
new
(
parser_ints
)
})
}
.parse
(
input
)
}
...
...
This diff is collapsed.
Click to expand it.
src/main.rs
+
9
−
7
View file @
edc52ccc
...
...
@@ -31,14 +31,16 @@ fn main() {
// let res = Expr.parse(_file.as_str());
// println!("{:#?}", res);
let
mut
count
=
0
;
let
max
=
200000
;
//
let mut count = 0;
//
let max = 200000;
loop
{
if
(
count
==
max
)
{
break
};
Expr
.parse
(
format!
(
"- 221 + {} + 8 11077770"
,
count
)
.as_str
());
count
+=
1
//
loop {
//
if (count == max) {break};
//
println!("{:#?}",
Expr.parse(format!("- 221 + {} + 8 11077770", count).as_str())
)
;
//
count += 1
}
// }
println!
(
"{:#?}"
,
Expr
.parse
(
format!
(
"221 - {} + 8 + 11077770"
,
4
)
.as_str
()));
}
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