Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
ion
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Enzo Cioppettini
ion
Commits
a7516959
Commit
a7516959
authored
6 years ago
by
Tormod Gjeitnes Hellen
Committed by
Michael Aaron Murphy
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Calc man page
parent
5ff10cac
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
members/builtins/src/calc.rs
+79
-26
79 additions, 26 deletions
members/builtins/src/calc.rs
with
79 additions
and
26 deletions
members/builtins/src/calc.rs
+
79
−
26
View file @
a7516959
use
calculate
::{
eval
,
eval_polish
,
CalcError
,
Value
};
use
std
::
io
::{
self
,
Write
};
const
REPL_GUIDE
:
&
'static
str
=
r#"ion-calc
Type in expressions to have them evaluated.
Type "help" for help."#
;
pub
const
MAN_CALC
:
&
str
=
r#"NAME
calc - Floating point calculator
SYNOPSIS
calc [EXPRESSION]
DESCRIPTION
Evaluates arithmetic expressions
SPECIAL EXPRESSIONS
help (only in interactive mode)
prints this help text
--help (only in non-interactive mode)
prints this help text
exit (only in interactive mode)
exits the program
NOTATIONS
infix notation
e.g. 3 * 4 + 5
polish notation
e.g. + * 3 4 5
EXAMPLES
Add two plus two in infix notation
calc 2+2
Add two plus two in polish notation
calc + 2 2
AUTHOR
Written by Hunter Goldstein.
"#
;
fn
calc_or_polish_calc
(
args
:
&
str
)
->
Result
<
Value
,
CalcError
>
{
match
eval
(
&
args
)
{
Ok
(
t
)
=>
Ok
(
t
),
...
...
@@ -11,35 +52,47 @@ fn calc_or_polish_calc(args: &str) -> Result<Value, CalcError> {
pub
fn
calc
(
args
:
&
[
String
])
->
Result
<
(),
String
>
{
let
stdout
=
io
::
stdout
();
let
mut
stdout
=
stdout
.lock
();
if
!
args
.is_empty
()
{
let
result
=
calc_or_polish_calc
(
&
args
.join
(
" "
));
let
_
=
match
result
{
Ok
(
v
)
=>
writeln!
(
stdout
,
"{}"
,
v
),
Err
(
e
)
=>
writeln!
(
stdout
,
"{}"
,
e
),
};
}
else
{
let
prompt
=
b"ion-calc: "
;
loop
{
let
_
=
stdout
.write
(
prompt
);
let
_
=
stdout
.flush
();
let
mut
input
=
String
::
new
();
let
_
=
io
::
stdin
()
.read_line
(
&
mut
input
);
if
input
.is_empty
()
{
break
;
}
else
{
match
input
.trim
()
{
""
=>
(),
"exit"
=>
break
,
s
=>
{
let
result
=
calc_or_polish_calc
(
s
);
let
_
=
match
result
{
Ok
(
v
)
=>
writeln!
(
stdout
,
"{}"
,
v
),
Err
(
e
)
=>
writeln!
(
stdout
,
"{}"
,
e
),
};
match
args
.first
()
{
Some
(
ref
s
)
if
(
*
s
==
"--help"
)
=>
{
// "--help" only makes sense if it is the first option. Only look for it
// in the first position.
println!
(
"{}"
,
MAN_CALC
);
Ok
(())
}
Some
(
_
)
=>
{
let
result
=
calc_or_polish_calc
(
&
args
.join
(
" "
));
let
_
=
match
result
{
Ok
(
v
)
=>
writeln!
(
stdout
,
"{}"
,
v
),
Err
(
e
)
=>
writeln!
(
stdout
,
"{}"
,
e
),
};
Ok
(())
}
None
=>
{
let
prompt
=
b"ion-calc: "
;
println!
(
"{}"
,
REPL_GUIDE
);
loop
{
let
_
=
stdout
.write
(
prompt
);
let
_
=
stdout
.flush
();
let
mut
input
=
String
::
new
();
let
_
=
io
::
stdin
()
.read_line
(
&
mut
input
);
if
input
.is_empty
()
{
break
;
}
else
{
match
input
.trim
()
{
""
=>
(),
"exit"
=>
break
,
"help"
=>
println!
(
"{}"
,
MAN_CALC
),
s
=>
{
let
result
=
calc_or_polish_calc
(
s
);
let
_
=
match
result
{
Ok
(
v
)
=>
writeln!
(
stdout
,
"{}"
,
v
),
Err
(
e
)
=>
writeln!
(
stdout
,
"{}"
,
e
),
};
}
}
}
}
Ok
(())
}
}
Ok
(())
}
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