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
90e746ef
Commit
90e746ef
authored
1 year ago
by
Michal Štěpánek
Browse files
Options
Downloads
Patches
Plain Diff
Fix internals => bc conversion
parent
638f995e
No related branches found
Branches containing commit
No related tags found
1 merge request
!4
Task 3 odevzdání
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/bc_compiler.c
+42
-18
42 additions, 18 deletions
src/bc_compiler.c
src/bc_compiler.h
+1
-1
1 addition, 1 deletion
src/bc_compiler.h
src/fml.c
+6
-2
6 additions, 2 deletions
src/fml.c
with
49 additions
and
21 deletions
src/bc_compiler.c
+
42
−
18
View file @
90e746ef
#include
"bc_compiler.h"
#include
<assert.h>
// DEBUG
#include
<stdio.h>
void
string_init
(
String
*
str
)
{
str
->
capacity
=
INIT_STRING_LENGTH
;
str
->
len
=
0
;
...
...
@@ -38,36 +41,41 @@ void string_write_i32 ( String * str, const i32 data ) {
void
string_write_constant
(
String
*
str
,
BCConstant
constant
)
{
switch
(
constant
.
kind
)
{
case
CONSTANT_NULL
:
// fprintf ( stderr, "null, " );
string_write_byte
(
str
,
0x01
);
break
;
case
CONSTANT_BOOLEAN
:
// fprintf ( stderr, "bool, " );
string_write_byte
(
str
,
0x04
);
string_write_byte
(
str
,
constant
.
boolean
?
0x01
:
0x00
);
break
;
case
CONSTANT_INTEGER
:
// fprintf ( stderr, "int, " );
string_write_byte
(
str
,
0x00
);
string_write_i32
(
str
,
constant
.
integer
);
break
;
case
CONSTANT_STRING
:
// fprintf ( stderr, "str, " );
string_write_byte
(
str
,
0x02
);
string_write_
byte
(
str
,
constant
.
string
.
len
);
string_write_
i32
(
str
,
constant
.
string
.
len
);
for
(
size_t
i
=
0
;
i
<
constant
.
string
.
len
;
++
i
)
string_write_byte
(
str
,
constant
.
string
.
str
[
i
]
);
break
;
case
CONSTANT_FUNCTION
:
// fprintf ( stderr, "f, " );
string_write_byte
(
str
,
0x03
);
string_write_byte
(
str
,
constant
.
function
.
parameters
);
string_write_u16
(
str
,
constant
.
function
.
locals
);
string_write_i32
(
str
,
constant
.
function
.
bc
.
len
);
for
(
size_t
i
=
0
;
i
<
constant
.
function
.
bc
.
len
;
++
i
)
string_write_byte
(
str
,
constant
.
function
.
bc
.
str
[
i
]
);
break
;
default:
assert
(
false
);
break
;
}
}
BCConstant
create_function
(
u8
parameters
)
{
BCFunction
function
=
(
BCFunction
)
{
.
parameters
=
parameters
,
.
locals
=
0
};
string_init
(
&
function
.
bc
);
...
...
@@ -100,7 +108,7 @@ u16 get_local_index ( BCCompilerState * state, Str name ) {
return
function
->
parameters
+
(
scope
->
prev
?
scope
->
prev
->
local_count
+
i
:
i
);
scope
=
scope
->
prev
;
}
u16
*
n
=
state
->
scope
->
local_count
;
u16
*
n
=
&
state
->
scope
->
local_count
;
assert
(
*
n
!=
256
*
256
);
state
->
scope
->
locals
[
*
n
]
=
name
;
++
(
*
n
);
...
...
@@ -111,9 +119,9 @@ u16 get_local_index ( BCCompilerState * state, Str name ) {
}
void
gen_bc_constant
(
BCCompilerState
*
state
,
u16
index
)
{
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
string_write_byte
(
function
->
bc
,
0x01
);
string_write_u16
(
function
->
bc
,
index
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
]
.
function
;
string_write_byte
(
&
function
->
bc
,
0x01
);
string_write_u16
(
&
function
->
bc
,
index
);
}
void
insert_constant
(
BCCompilerState
*
state
,
BCConstant
constant
)
{
...
...
@@ -132,6 +140,9 @@ void bc_state_init ( BCCompilerState * state ) {
void
bc_state_destroy
(
BCCompilerState
*
state
)
{
// TODO ( free strings )
for
(
size_t
i
=
0
;
i
<
state
->
constants
.
constant_count
;
++
i
)
if
(
state
->
constants
.
constants
[
i
]
.
kind
==
CONSTANT_STRING
)
string_destroy
(
&
state
->
constants
.
constants
[
i
]
.
function
.
bc
);
}
void
ast_to_bc
(
BCCompilerState
*
state
,
Ast
*
ast
)
{
...
...
@@ -160,11 +171,12 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
AstInteger
*
integer
=
(
AstInteger
*
)
ast
;
u16
i
;
for
(
i
=
0
;
i
<
state
->
constants
.
constant_count
;
++
i
)
if
(
state
->
constants
.
constants
[
i
]
.
kind
==
CONSTANT_INTEGER
&&
state
->
constants
.
constants
[
i
]
.
integer
==
integer
->
value
)
;
if
(
state
->
constants
.
constants
[
i
]
.
kind
==
CONSTANT_INTEGER
&&
state
->
constants
.
constants
[
i
]
.
integer
==
integer
->
value
)
break
;
if
(
i
==
state
->
constants
.
constant_count
;
++
i
)
if
(
i
==
state
->
constants
.
constant_count
)
insert_constant
(
state
,
(
BCConstant
)
{
.
kind
=
CONSTANT_INTEGER
,
.
integer
=
integer
->
value
}
);
gen_bc_constant
(
state
,
i
);
return
;
}
case
AST_BOOLEAN
:
{
AstBoolean
*
boolean
=
(
AstBoolean
*
)
ast
;
...
...
@@ -174,6 +186,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
insert_constant
(
state
,
(
BCConstant
)
{
.
kind
=
CONSTANT_BOOLEAN
,
.
boolean
=
boolean
->
value
}
);
}
gen_bc_constant
(
state
,
state
->
constants
.
bool_pos
[
pos
]
);
return
;
}
case
AST_BLOCK
:
{
AstBlock
*
block
=
(
AstBlock
*
)
ast
;
...
...
@@ -183,8 +196,8 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
scope
->
used_locals
=
0
;
scope
->
prev
=
state
->
scope
;
state
->
scope
=
scope
;
for
(
size_t
i
=
0
;
i
<
top
->
expression_cnt
;
++
i
)
ast_to_bc
(
state
,
top
->
expressions
[
i
]
);
for
(
size_t
i
=
0
;
i
<
block
->
expression_cnt
;
++
i
)
ast_to_bc
(
state
,
block
->
expressions
[
i
]
);
// delete local scope
state
->
scope
=
state
->
scope
->
prev
;
free
(
scope
);
...
...
@@ -195,12 +208,12 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
LocalScope
*
old_scope
=
state
->
scope
;
u16
old_ep
=
state
->
ep
;
state
->
ep
=
state
->
fp
=
state
->
constants
.
constant_count
;
insert_constant
(
state
,
create_function
(
func
->
parameter
s
)
);
state
->
scope
=
create_function_scope
(
func
->
parameter
s
);
insert_constant
(
state
,
create_function
(
func
->
parameter
_cnt
)
);
state
->
scope
=
create_function_scope
(
func
->
parameter
_cnt
);
state
->
scope
->
prev
=
NULL
;
state
->
scope
->
used_locals
=
func
->
parameter
s
;
state
->
scope
->
local_count
=
func
->
parameter
s
;
for
(
u16
i
=
1
;
i
<
func
->
parameter
s
)
state
->
scope
->
used_locals
=
func
->
parameter
_cnt
;
state
->
scope
->
local_count
=
func
->
parameter
_cnt
;
for
(
u16
i
=
1
;
i
<
func
->
parameter
_cnt
;
++
i
)
state
->
scope
->
locals
[
i
]
=
func
->
parameters
[
i
];
ast_to_bc
(
state
,
func
->
body
);
free
(
state
->
scope
);
...
...
@@ -324,19 +337,30 @@ String generate_bc ( Ast * ast ) {
string_write_byte
(
&
bc
,
'F'
);
string_write_byte
(
&
bc
,
'M'
);
string_write_byte
(
&
bc
,
'L'
);
string_write_byte
(
&
bc
,
'\0'
);
string_write_byte
(
&
bc
,
'\n'
);
// fprintf ( stderr, "printed FML\n" );
// constants to bc
// fprintf ( stderr, "%d constants:\n", bc_state . constants . constant_count );
string_write_u16
(
&
bc
,
bc_state
.
constants
.
constant_count
);
for
(
size_t
i
=
0
;
i
<
bc_state
.
constants
.
constant_count
;
++
i
)
string_write_constant
(
&
bc
,
bc_state
.
constants
.
constants
[
i
]
);
// fprintf ( stderr, "\n" );
// globals to bc
// fprintf ( stderr, "%u globals:\n", bc_state . globals . global_count );
string_write_u16
(
&
bc
,
bc_state
.
globals
.
global_count
);
for
(
size_t
i
=
0
;
i
<
bc_state
.
globals
.
global_count
;
++
i
)
for
(
size_t
i
=
0
;
i
<
bc_state
.
globals
.
global_count
;
++
i
)
{
// fprintf ( stderr, "%u, ", bc_state . globals . names [ i ] );
string_write_u16
(
&
bc
,
bc_state
.
globals
.
names
[
i
]
);
}
// fprintf ( stderr, "\n" );
// EP
string_write_u16
(
&
bc
,
bc_state
.
ep
);
// fprintf ( stderr, "EP=%u\n", bc_state . ep );
// free
bc_state_init_destroy
(
&
bc_state
);
bc_state_destroy
(
&
bc_state
);
// fprintf ( stderr, "len: %u, %.*s\n", bc . len, (int) bc . len, bc . str );
return
bc
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/bc_compiler.h
+
1
−
1
View file @
90e746ef
...
...
@@ -82,4 +82,4 @@ void bc_state_init ( BCCompilerState * state );
void bc_state_destroy ( BCCompilerState * state );
void ast_to_bc ( BCCompilerState * state, Ast * ast );
void generate_bc ( Ast * ast );
\ No newline at end of file
String generate_bc ( Ast * ast );
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/fml.c
+
6
−
2
View file @
90e746ef
...
...
@@ -151,8 +151,12 @@ int main ( int argc, char **argv ) {
if
(
bc_compiler
)
{
if
(
!
bc_interpreter
)
printf
(
"%.*s"
,
(
int
)
bc
.
len
,
bc
.
str
);
for
(
size_t
i
=
0
;
i
<
bc
.
len
;
++
i
)
{
printf
(
"%c"
,
bc
.
str
[
i
]
);
//printf ( "%u ", bc . str [ i ] );
}
//printf ( "%.*s", (int) bc . len, bc . str );
//printf ( "\n" );
string_destroy
(
&
bc
);
}
...
...
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