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
7dc4bca2
Commit
7dc4bca2
authored
1 year ago
by
Michal Štěpánek
Browse files
Options
Downloads
Patches
Plain Diff
Finish AST => BC structures conv. for subtask1
parent
4aa553fe
No related branches found
No related tags found
1 merge request
!4
Task 3 odevzdání
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/bc_compiler.c
+77
-0
77 additions, 0 deletions
src/bc_compiler.c
src/bc_compiler.h
+3
-2
3 additions, 2 deletions
src/bc_compiler.h
with
80 additions
and
2 deletions
src/bc_compiler.c
+
77
−
0
View file @
7dc4bca2
...
...
@@ -57,6 +57,26 @@ u16 get_string_index ( BCCompilerState * state, Str str ) {
return
i
;
}
u16
get_local_index
(
BCCompilerState
*
state
,
Str
name
)
{
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
LocalScope
*
scope
=
state
->
scope
;
size_t
i
;
while
(
scope
)
{
for
(
i
=
0
;
i
<
scope
->
used_locals
;
++
i
)
if
(
str_eq
(
scope
->
locals
[
i
],
name
)
)
return
function
->
parameters
+
(
scope
->
prev
?
scope
->
prev
->
local_count
+
i
);
scope
=
scope
->
prev
;
}
u32
*
n
=
state
->
scope
->
local_count
;
assert
(
*
n
!=
256
*
256
);
state
->
scope
->
locals
[
*
n
]
=
name
;
++
(
*
n
);
++
(
state
->
scope
->
used_locals
);
if
(
*
n
>
function
->
locals
)
function
->
locals
=
*
n
;
return
*
n
-
1
+
function
->
parameters
;
}
void
gen_bc_constant
(
BCCompilerState
*
state
,
u16
index
)
{
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
function
->
bc
,
0x01
);
...
...
@@ -176,6 +196,63 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
string_write_byte
(
&
function
->
bc
,
print
->
argument_cnt
&
255
);
return
;
}
case
AST_DEFINITION
:
{
AstDefinition
*
def
=
(
AstDefinition
*
)
ast
;
ast_to_bc
(
state
,
def
->
value
);
if
(
!
state
->
scope
->
prev
)
{
// GLOBAL
u16
index
=
get_string_index
(
state
,
def
->
name
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
&
function
->
bc
,
0x0B
);
string_write_u16
(
&
function
->
bc
,
index
);
}
else
{
// LOCAL
u16
index
=
get_local_index
(
state
,
def
->
name
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
&
function
->
bc
,
0x09
);
string_write_u16
(
&
function
->
bc
,
index
);
}
return
;
}
case
AST_VARIABLE_ACCESS
:
{
AstVariableAccess
*
var
=
(
AstVariableAccess
*
)
ast
;
if
(
!
state
->
scope
->
prev
)
{
// GLOBAL
u16
index
=
get_string_index
(
state
,
var
->
name
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
&
function
->
bc
,
0x0C
);
string_write_u16
(
&
function
->
bc
,
index
);
}
else
{
// LOCAL
u16
index
=
get_local_index
(
state
,
var
->
name
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
&
function
->
bc
,
0x0A
);
string_write_u16
(
&
function
->
bc
,
index
);
}
return
;
}
case
AST_VARIABLE_ASSIGNMENT
:
{
AstVariableAssignment
*
assign
=
(
AstVariableAssignment
*
)
ast
;
ast_to_bc
(
state
,
assign
->
value
);
if
(
!
state
->
scope
->
prev
)
{
// GLOBAL
u16
constants
=
state
->
constants
.
constant_count
;
u16
index
=
get_string_index
(
state
,
assign
->
name
);
assert
(
constants
==
state
->
constants
.
constant_count
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
&
function
->
bc
,
0x0B
);
string_write_u16
(
&
function
->
bc
,
index
);
}
else
{
// LOCAL
u16
locals
=
state
->
scope
->
local_count
;
u16
index
=
get_local_index
(
state
,
assign
->
name
);
assert
(
locals
==
state
->
scope
->
local_count
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
&
function
->
bc
,
0x09
);
string_write_u16
(
&
function
->
bc
,
index
);
}
return
;
}
default:
assert
(
false
);
}
...
...
This diff is collapsed.
Click to expand it.
src/bc_compiler.h
+
3
−
2
View file @
7dc4bca2
...
...
@@ -55,8 +55,8 @@ typedef struct Globals {
typedef struct LocalScope {
struct LocalScope * prev;
u16 used_locals;
u
32
local_count;
u16 used_locals;
// locals at use in this scope
u
16
local_count;
// locals at use at this time
Str locals [];
} LocalScope;
...
...
@@ -72,6 +72,7 @@ BCConstant create_function ( Str name, u8 parameters );
LocalScope * create_function_scope ( u8 parameters );
u16 get_string_index ( BCCompilerState * state, Str str );
u16 get_local_index ( BCCompilerState * state, Str name );
void gen_bc_constant ( BCCompilerState * state, u16 index );
void insert_constant ( BCCompilerState * state, Constant constant );
...
...
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