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
d74c0536
Commit
d74c0536
authored
1 year ago
by
Michal Štěpánek
Browse files
Options
Downloads
Patches
Plain Diff
Code cleanup
parent
dc189082
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
+5
-57
5 additions, 57 deletions
src/bc_compiler.c
src/fml.c
+0
-3
0 additions, 3 deletions
src/fml.c
with
5 additions
and
60 deletions
src/bc_compiler.c
+
5
−
57
View file @
d74c0536
#include
"bc_compiler.h"
#include
<assert.h>
// DEBUG
#include
<stdio.h>
void
string_init
(
String
*
str
)
{
str
->
capacity
=
INIT_STRING_LENGTH
;
str
->
len
=
0
;
...
...
@@ -41,28 +38,23 @@ 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: %u, ", constant . boolean );
string_write_byte
(
str
,
0x04
);
string_write_byte
(
str
,
constant
.
boolean
?
0x01
:
0x00
);
break
;
case
CONSTANT_INTEGER
:
// fprintf ( stderr, "int: %d, ", constant . integer );
string_write_byte
(
str
,
0x00
);
string_write_i32
(
str
,
constant
.
integer
);
break
;
case
CONSTANT_STRING
:
// fprintf ( stderr, "\"%.*s\", ", (int) constant . string . len, constant . string . str );
string_write_byte
(
str
,
0x02
);
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 ( %u, %u, %u ), ", constant . function . parameters, constant . function . locals, constant . function . bc . len );
string_write_byte
(
str
,
0x03
);
string_write_byte
(
str
,
constant
.
function
.
parameters
);
string_write_u16
(
str
,
constant
.
function
.
locals
);
...
...
@@ -227,7 +219,6 @@ void gen_cond_body ( BCCompilerState * state, Ast * cons, Ast * alt ) {
u16
alt_len
=
cons_pos
-
alt_pos
;
function
->
bc
.
str
[
alt_pos
-
2
]
=
alt_len
&
255
;
function
->
bc
.
str
[
alt_pos
-
1
]
=
(
alt_len
>>
8
)
&
255
;
//string_write_byte ( & function -> bc, 0 );
add_scope
(
state
);
ast_to_bc
(
state
,
cons
);
remove_scope
(
state
);
...
...
@@ -245,7 +236,6 @@ void gen_loop ( BCCompilerState * state, Ast * cond, Ast * body ) {
string_write_byte
(
&
function
->
bc
,
0x0E
);
string_write_u16
(
&
function
->
bc
,
0
);
size_t
body_pos
=
function
->
bc
.
len
;
//string_write_byte ( & function -> bc, 0x00 );
add_scope
(
state
);
ast_to_bc
(
state
,
body
);
remove_scope
(
state
);
...
...
@@ -270,7 +260,6 @@ void gen_loop_from_bc ( BCCompilerState * state, String * cond, String * body )
string_write_byte
(
&
function
->
bc
,
0x0E
);
string_write_u16
(
&
function
->
bc
,
0
);
size_t
body_pos
=
function
->
bc
.
len
;
//string_write_byte ( & function -> bc, 0x00 );
for
(
size_t
i
=
0
;
i
<
body
->
len
;
++
i
)
string_write_byte
(
&
function
->
bc
,
body
->
str
[
i
]
);
string_write_byte
(
&
function
->
bc
,
0x0E
);
...
...
@@ -288,17 +277,10 @@ void gen_bc_array_init ( BCCompilerState * state, AstArray * array, u16 size_ind
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
]
.
function
;
u16
array_index
=
make_local_index
(
state
,
STR
(
"
\0
arr"
)
);
gen_bc_set_local
(
&
function
->
bc
,
array_index
);
//string_write_byte ( & function -> bc, 0 );
u16
one_index
=
get_or_make_int
(
state
,
1
);
//AstInteger one = (AstInteger) { .base = (Ast) { .kind = AST_INTEGER }, .value = 1 };
// string_write_byte ( & function -> bc, 0 );
// ast_to_bc ( state, & one . base );
//u16 one_index = (function -> bc . str [ function -> bc . len - 2 ] & 255) + ((function -> bc . str [ function -> bc . len - 1 ] & 255) << 8);
gen_bc_constant
(
state
,
one_index
);
u16
i_index
=
make_local_index
(
state
,
STR
(
"
\0
i"
)
);
gen_bc_set_local
(
&
function
->
bc
,
i_index
);
// string_write_byte ( & function -> bc, 0x09 );
// string_write_u16 ( & function -> bc, i_index );
string_write_byte
(
&
function
->
bc
,
0
);
String
cond
,
body
;
string_init
(
&
cond
);
...
...
@@ -306,10 +288,6 @@ void gen_bc_array_init ( BCCompilerState * state, AstArray * array, u16 size_ind
// create cond BC
gen_bc_get_local
(
&
cond
,
i_index
);
gen_bc_get_local
(
&
cond
,
size_index
);
// string_write_byte ( & cond, 0x0A );
// string_write_u16 ( & cond, i_index );
// string_write_byte ( & cond, 0x0A );
// string_write_u16 ( & cond, size_index );
// call < operator to compare size and index
u16
index
=
get_string_index
(
state
,
STR
(
"<"
)
);
if
(
index
==
0
)
...
...
@@ -320,17 +298,13 @@ void gen_bc_array_init ( BCCompilerState * state, AstArray * array, u16 size_ind
// create body BC
gen_bc_get_local
(
&
body
,
array_index
);
gen_bc_get_local
(
&
body
,
i_index
);
// string_write_byte ( & body, 0x0A );
// string_write_u16 ( & body, array_index );
// string_write_byte ( & body, 0x0A );
// string_write_u16 ( & body, i_index );
size_t
pos
=
function
->
bc
.
len
;
add_scope
(
state
);
ast_to_bc
(
state
,
array
->
initializer
);
remove_scope
(
state
);
// value BC was created in function body, move to correct string
for
(
size_t
i
=
0
;
i
<
function
->
bc
.
len
-
pos
;
++
i
)
string_write_byte
(
&
body
,
function
->
bc
.
str
[
pos
+
i
]
);
for
(
size_t
i
=
pos
;
i
<
function
->
bc
.
len
;
++
i
)
string_write_byte
(
&
body
,
function
->
bc
.
str
[
i
]
);
function
->
bc
.
len
=
pos
;
// index assignment
index
=
get_string_index
(
state
,
STR
(
"set"
)
);
...
...
@@ -345,23 +319,17 @@ void gen_bc_array_init ( BCCompilerState * state, AstArray * array, u16 size_ind
if
(
index
==
0
)
index
=
make_string_index
(
state
,
STR
(
"+"
)
);
gen_bc_get_local
(
&
body
,
i_index
);
// string_write_byte ( & body, 0x0A );
// string_write_u16 ( & body, i_index );
string_write_byte
(
&
body
,
0x01
);
string_write_u16
(
&
body
,
one_index
);
string_write_byte
(
&
body
,
0x07
);
string_write_u16
(
&
body
,
index
);
string_write_byte
(
&
body
,
2
);
gen_bc_set_local
(
&
body
,
i_index
);
// string_write_byte ( & body, 0x09 );
// string_write_u16 ( & body, i_index );
string_write_byte
(
&
body
,
0
);
gen_loop_from_bc
(
state
,
&
cond
,
&
body
);
//gen_bc_get_local ( & function -> bc, array_index );
// string_write_byte ( & function -> bc, 0x0A );
// string_write_u16 ( & function -> bc, array_index );
gen_loop_from_bc
(
state
,
&
cond
,
&
body
);
string_destroy
(
&
cond
);
string_destroy
(
&
body
);
gen_bc_get_local
(
&
function
->
bc
,
array_index
);
}
void
gen_bc_get_local
(
String
*
body
,
u16
index
)
{
...
...
@@ -393,7 +361,6 @@ void bc_state_init ( BCCompilerState * state ) {
state
->
globals
.
global_count
=
0
;
state
->
scope
=
NULL
;
state
->
side_effect
=
false
;
//state -> fp = 0;
}
void
bc_state_destroy
(
BCCompilerState
*
state
)
{
...
...
@@ -529,8 +496,6 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
if
(
index
==
INVALID_LOCAL
)
index
=
make_local_index
(
state
,
def
->
name
);
gen_bc_set_local
(
&
function
->
bc
,
index
);
// string_write_byte ( & function -> bc, 0x09 );
// string_write_u16 ( & function -> bc, index );
}
return
;
}
...
...
@@ -555,8 +520,6 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
}
else
{
index
=
make_local_index
(
state
,
var
->
name
);
gen_bc_get_local
(
&
function
->
bc
,
index
);
// string_write_byte ( & function -> bc, 0x0A );
// string_write_u16 ( & function -> bc, index );
return
;
}
}
...
...
@@ -572,9 +535,6 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
}
else
{
// LOCAL
gen_bc_get_local
(
&
function
->
bc
,
index
);
// string_write_byte ( & function -> bc, 0x0A );
// string_write_u16 ( & function -> bc, index );
}
return
;
}
...
...
@@ -601,8 +561,6 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
}
else
{
index
=
make_local_index
(
state
,
assign
->
name
);
gen_bc_set_local
(
&
function
->
bc
,
index
);
// string_write_byte ( & function -> bc, 0x09 );
// string_write_u16 ( & function -> bc, index );
return
;
}
}
...
...
@@ -618,8 +576,6 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
}
else
{
// LOCAL
gen_bc_set_local
(
&
function
->
bc
,
index
);
// string_write_byte ( & function -> bc, 0x09 );
// string_write_u16 ( & function -> bc, index );
}
return
;
}
...
...
@@ -764,7 +720,6 @@ String generate_bc ( Ast * ast ) {
string_write_byte
(
&
bc
,
'M'
);
string_write_byte
(
&
bc
,
'L'
);
string_write_byte
(
&
bc
,
'\n'
);
// fprintf ( stderr, "printed FML\n" );
// constants to bc
// first functions to bc internals
for
(
size_t
i
=
0
;
i
<
bc_state
.
constants
.
constant_count
;
++
i
)
...
...
@@ -774,23 +729,16 @@ String generate_bc ( Ast * ast ) {
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
)
{
// 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_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/fml.c
+
0
−
3
View file @
d74c0536
...
...
@@ -154,10 +154,7 @@ int main ( int argc, char **argv ) {
if
(
!
bc_interpreter
)
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