Skip to main content
Homepage
Explore
Search or go to…
/
Sign in
Explore
Primary navigation
Project
C
compilerLaLog
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
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Collapse sidebar
Snippets
Groups
Projects
Show more breadcrumbs
JAUJAY Augustin
compilerLaLog
Commits
589b4a25
Commit
589b4a25
authored
Mar 20, 2022
by
Augustin Jaujay
Browse files
Options
Downloads
Patches
Plain Diff
Instruction Exec fonctionnelle
parent
6e082ac0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
pfx/basic/ast.mli
+2
-0
2 additions, 0 deletions
pfx/basic/ast.mli
pfx/basic/eval.ml
+37
-14
37 additions, 14 deletions
pfx/basic/eval.ml
pfx/basic/eval.mli
+5
-1
5 additions, 1 deletion
pfx/basic/eval.mli
with
44 additions
and
15 deletions
pfx/basic/ast.mli
+
2
−
0
View file @
589b4a25
...
...
@@ -7,5 +7,7 @@ type program = int * command list
(* Converting a command to a string for printing *)
val
string_of_command
:
command
->
string
val
string_of_commands
:
command
list
->
string
(* Converting a program to a string for printing *)
val
string_of_program
:
program
->
string
This diff is collapsed.
Click to expand it.
pfx/basic/eval.ml
+
37
−
14
View file @
589b4a25
open
Ast
open
Printf
let
string_of_stack
stack
=
sprintf
"[%s]"
(
String
.
concat
";"
(
List
.
map
string_of_int
stack
))
type
argsType
=
Int
of
int
|
InstructionSeq
of
command
list
let
rec
string_of_stack
stack
=
match
stack
with
|
[]
->
""
|
(
Int
i
)
::
s
->
(
string_of_int
i
)
^
string_of_stack
s
|
(
InstructionSeq
seq
)
::
s
->
(
string_of_commands
seq
)
^
string_of_stack
s
let
string_of_state
(
cmds
,
stack
)
=
(
match
cmds
with
...
...
@@ -19,30 +24,43 @@ let step state =
|
Swap
::_,
[]
->
Error
(
"Nothing to swap"
,
state
)
|
Add
::_,
_
::
[]
->
Error
(
"Nothing to add"
,
state
)
|
Add
::_,
[]
->
Error
(
"Nothing to add"
,
state
)
|
Add
::_,
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Add
::_,
_
::
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Sub
::_,
_
::
[]
->
Error
(
"Nothing to substract"
,
state
)
|
Sub
::_,
[]
->
Error
(
"Nothing to substract"
,
state
)
|
Sub
::_,
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Sub
::_,
_
::
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Mul
::_,
_
::
[]
->
Error
(
"Nothing to multiply"
,
state
)
|
Mul
::_,
[]
->
Error
(
"Nothing to multiply"
,
state
)
|
Mul
::_,
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Mul
::_,
_
::
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Div
::_,
_
::
[]
->
Error
(
"Nothing to divide"
,
state
)
|
Div
::_,
[]
->
Error
(
"Nothing to divide"
,
state
)
|
Div
::_,
_
::
0
::_
->
Error
(
"Forbidden operation"
,
state
)
|
Div
::_,
_
::
(
Int
0
)
::_
->
Error
(
"Forbidden operation"
,
state
)
|
Div
::_,
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Div
::_,
_
::
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Rem
::_,
_
::
[]
->
Error
(
"Nothing to divide"
,
state
)
|
Rem
::_,
[]
->
Error
(
"Nothing to divide"
,
state
)
|
Rem
::_,
_
::
0
::_
->
Error
(
"Forbidden operation"
,
state
)
|
Get
::_,
i
::
stack
when
(
List
.
length
stack
)
<
i
->
Error
(
"Invalid get operation"
,
state
)
|
Get
::_,
i
::_
when
i
<
0
->
Error
(
"Cannot get negative index"
,
state
)
|
Rem
::_,
_
::
(
Int
0
)
::_
->
Error
(
"Forbidden operation"
,
state
)
|
Rem
::_,
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Rem
::_,
_
::
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Get
::_,
(
Int
i
)
::
stack
when
(
List
.
length
stack
)
<
i
->
Error
(
"Invalid get operation"
,
state
)
|
Get
::_,
(
Int
i
)
::_
when
i
<
0
->
Error
(
"Cannot get negative index"
,
state
)
|
Get
::_,
[]
->
Error
(
"Nothing to get"
,
state
)
|
Exec
::_,
_
->
Error
(
"Not yet supported"
,
state
)
(* TODO *)
|
Get
::_,
(
InstructionSeq
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
|
Exec
::_,
[]
->
Error
(
"Nothing to exec"
,
state
)
|
Exec
::_,
(
Int
_
)
::_
->
Error
(
"Wrong arg type"
,
state
)
(* Valid configurations *)
|
(
Push
x
)
::
q
,
stack
->
Ok
(
q
,
x
::
stack
)
|
(
Push
x
)
::
q
,
stack
->
Ok
(
q
,
(
Int
x
)
::
stack
)
|
Pop
::
q
,
_
::
stack
->
Ok
(
q
,
stack
)
|
Swap
::
q
,
x
::
y
::
stack
->
Ok
(
q
,
y
::
x
::
stack
)
|
Add
::
q
,
x
::
y
::
stack
->
Ok
(
q
,
(
x
+
y
)
::
stack
)
|
Sub
::
q
,
x
::
y
::
stack
->
Ok
(
q
,
(
x
-
y
)
::
stack
)
|
Mul
::
q
,
x
::
y
::
stack
->
Ok
(
q
,
(
x
*
y
)
::
stack
)
|
Div
::
q
,
x
::
y
::
stack
->
Ok
(
q
,
(
x
/
y
)
::
stack
)
|
Rem
::
q
,
x
::
y
::
stack
->
Ok
(
q
,
(
x
mod
y
)
::
stack
)
|
Get
::
q
,
i
::
stack
->
Ok
(
q
,
(
List
.
nth
stack
i
)
::
stack
)
|
Add
::
q
,
(
Int
x
)
::
(
Int
y
)
::
stack
->
Ok
(
q
,
(
Int
(
x
+
y
))
::
stack
)
|
Sub
::
q
,
(
Int
x
)
::
(
Int
y
)
::
stack
->
Ok
(
q
,
(
Int
(
x
-
y
))
::
stack
)
|
Mul
::
q
,
(
Int
x
)
::
(
Int
y
)
::
stack
->
Ok
(
q
,
(
Int
(
x
*
y
))
::
stack
)
|
Div
::
q
,
(
Int
x
)
::
(
Int
y
)
::
stack
->
Ok
(
q
,
(
Int
(
x
/
y
))
::
stack
)
|
Rem
::
q
,
(
Int
x
)
::
(
Int
y
)
::
stack
->
Ok
(
q
,
(
Int
(
x
mod
y
))
::
stack
)
|
Get
::
q
,
(
Int
i
)
::
stack
->
Ok
(
q
,
(
List
.
nth
stack
i
)
::
stack
)
|
Exec
::
q
,
(
InstructionSeq
s
)
::
stack
->
Ok
(
s
@
q
,
stack
)
let
eval_program
(
numargs
,
cmds
)
args
=
let
rec
execute
=
function
...
...
@@ -58,6 +76,11 @@ let eval_program (numargs, cmds) args =
if
numargs
=
List
.
length
args
then
match
execute
(
cmds
,
args
)
with
|
Ok
None
->
printf
"No result
\n
"
|
Ok
(
Some
result
)
->
printf
"= %i
\n
"
result
|
Ok
(
Some
result
)
->
begin
match
result
with
|
Int
x
->
printf
"= %i
\n
"
x
|
InstructionSeq
_
->
printf
"No result
\n
"
end
|
Error
(
msg
,
s
)
->
printf
"Raised error %s in state %s
\n
"
msg
(
string_of_state
s
)
else
printf
"Raised error
\n
Mismatch between expected and actual number of args
\n
"
This diff is collapsed.
Click to expand it.
pfx/basic/eval.mli
+
5
−
1
View file @
589b4a25
val
eval_program
:
Ast
.
program
->
int
list
->
unit
open
Ast
type
argsType
=
Int
of
int
|
InstructionSeq
of
command
list
val
eval_program
:
Ast
.
program
->
argsType
list
->
unit
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
sign in
to comment