Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NI-RUN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michal Štěpánek
NI-RUN
Commits
696bdcb9
Commit
696bdcb9
authored
2 years ago
by
Michal Štěpánek
Browse files
Options
Downloads
Patches
Plain Diff
Finish subtask 1
parent
496c2868
No related branches found
Branches containing commit
No related tags found
Loading
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/ast_interpreter.c
+40
-7
40 additions, 7 deletions
src/ast_interpreter.c
src/ast_interpreter.h
+4
-1
4 additions, 1 deletion
src/ast_interpreter.h
with
44 additions
and
8 deletions
src/ast_interpreter.c
+
40
−
7
View file @
696bdcb9
...
...
@@ -97,13 +97,13 @@ Value env_get ( ASTInterpreterState * state, Str name ) {
}
// fprintf ( stderr, "Variable with name %.*s does not exist.\n", (int) name . len, name . str );
// exit ( 1 );
return
(
Value
)
{
.
kind
=
VALUE_NULL
};
//
env = state -> current_env;
//
env = env ? env : & state -> global_env;
//
entry = (EnvironmentEntry*) malloc ( sizeof (EnvironmentEntry) );
//
(*entry) = (EnvironmentEntry) {.name = name, .value = (Value) { .kind = VALUE_NULL }, .next = env -> start };
//
env -> start = entry;
//
return entry -> value;
//
return (Value) { .kind = VALUE_NULL };
env
=
state
->
current_env
;
env
=
env
?
env
:
&
state
->
global_env
;
entry
=
(
EnvironmentEntry
*
)
malloc
(
sizeof
(
EnvironmentEntry
)
);
(
*
entry
)
=
(
EnvironmentEntry
)
{.
name
=
name
,
.
value
=
(
Value
)
{
.
kind
=
VALUE_NULL
},
.
next
=
env
->
start
};
env
->
start
=
entry
;
return
entry
->
value
;
}
void
*
heap_alloc
(
Heap
*
heap
,
size_t
len
,
size_t
align
)
{
...
...
@@ -157,6 +157,29 @@ void state_destroy ( ASTInterpreterState * state ) {
}
}
Value
function_call
(
ASTInterpreterState
*
state
,
Value
function
,
bool
is_function
,
Ast
**
arguments
,
size_t
argc
)
{
Value
*
values
=
(
Value
*
)
malloc
(
argc
*
sizeof
(
Value
)
);
for
(
size_t
i
=
0
;
i
<
argc
;
++
i
)
values
[
i
]
=
evaluate
(
state
,
arguments
[
i
]
);
if
(
is_function
)
{
if
(
function
.
kind
!=
VALUE_FUNCTION
)
{
fprintf
(
stderr
,
"Invalid calee.
\n
"
);
exit
(
4
);
}
Environment
*
tmp
=
state
->
current_env
;
state
->
current_env
=
NULL
;
env_push
(
state
);
env_def
(
state
,
STR
(
"this"
),
(
Value
)
{
.
kind
=
VALUE_NULL
}
);
for
(
size_t
i
=
0
;
i
<
argc
;
++
i
)
env_def
(
state
,
function
.
function
->
parameters
[
i
],
values
[
i
]
);
Value
ret
=
evaluate
(
state
,
function
.
function
->
body
);
env_pop
(
state
);
state
->
current_env
=
tmp
;
return
ret
;
}
return
(
Value
)
{
.
kind
=
VALUE_NULL
};
}
void
print_value
(
Value
value
)
{
switch
(
value
.
kind
)
{
case
VALUE_INTEGER
:
...
...
@@ -171,6 +194,9 @@ void print_value ( Value value ) {
case
VALUE_NULL
:
printf
(
"null"
);
break
;
case
VALUE_FUNCTION
:
printf
(
"function"
);
break
;
}
}
...
...
@@ -282,6 +308,13 @@ Value evaluate ( ASTInterpreterState * state, Ast * ast ) {
}
return
(
Value
)
{
.
kind
=
VALUE_NULL
};
}
case
AST_FUNCTION
:
return
(
Value
)
{
.
kind
=
VALUE_FUNCTION
,
.
function
=
(
AstFunction
*
)
ast
};
case
AST_FUNCTION_CALL
:
{
AstFunctionCall
*
callAst
=
(
AstFunctionCall
*
)
ast
;
Value
function
=
evaluate
(
state
,
callAst
->
function
);
return
function_call
(
state
,
function
,
true
,
callAst
->
arguments
,
callAst
->
argument_cnt
);
}
}
return
(
Value
)
{
.
kind
=
VALUE_NULL
};
}
This diff is collapsed.
Click to expand it.
src/ast_interpreter.h
+
4
−
1
View file @
696bdcb9
...
...
@@ -3,7 +3,8 @@
typedef enum {
VALUE_INTEGER,
VALUE_BOOLEAN,
VALUE_NULL
VALUE_NULL,
VALUE_FUNCTION
} ValueType;
typedef struct Heap {
...
...
@@ -17,6 +18,7 @@ typedef struct Value {
union {
i32 integer;
bool boolean;
AstFunction * function;
};
} Value;
...
...
@@ -50,6 +52,7 @@ void heap_destroy ( Heap * heap );
void state_init ( ASTInterpreterState * state, Heap * heap );
void state_destroy ( ASTInterpreterState * state );
Value function_call ( ASTInterpreterState * state, Value function, bool is_function, Ast ** arguments, size_t argc );
void print_value ( Value value );
void fml_print ( Str format, Value * args, size_t argc );
Value evaluate ( ASTInterpreterState * state, Ast * ast );
\ No newline at end of file
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