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
4dfe740d
Commit
4dfe740d
authored
1 year ago
by
Michal Štěpánek
Browse files
Options
Downloads
Patches
Plain Diff
Add bc structures => string conv.
parent
7dc4bca2
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
+68
-3
68 additions, 3 deletions
src/bc_compiler.c
src/bc_compiler.h
+2
-1
2 additions, 1 deletion
src/bc_compiler.h
with
70 additions
and
4 deletions
src/bc_compiler.c
+
68
−
3
View file @
4dfe740d
...
...
@@ -35,6 +35,39 @@ void string_write_i32 ( String * str, const i32 data ) {
string_write_byte
(
str
,
(
data
>>
24
)
&
255
);
}
void
string_write_constant
(
String
*
str
,
BCConstant
constant
)
{
switch
(
constant
.
kind
)
{
case
CONSTANT_NULL
:
string_write_byte
(
str
,
0x01
);
break
;
case
CONSTANT_BOOLEAN
:
string_write_byte
(
str
,
0x04
);
string_write_byte
(
str
,
constant
.
boolean
?
0x01
:
0x00
);
break
;
case
CONSTANT_INTEGER
:
string_write_byte
(
str
,
0x00
);
string_write_i32
(
str
,
constant
.
integer
);
break
;
case
CONSTANT_STRING
:
string_write_byte
(
str
,
0x02
);
string_write_byte
(
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
:
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
]
);
default:
assert
(
false
);
break
;
}
}
BCConstant
create_function
(
u8
parameters
)
{
BCFunction
function
=
(
BCFunction
)
{
.
parameters
=
parameters
,
.
locals
=
0
};
string_init
(
&
function
.
bc
);
...
...
@@ -203,6 +236,12 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
// GLOBAL
u16
index
=
get_string_index
(
state
,
def
->
name
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
u16
*
count
=
&
state
->
globals
.
global_count
;
for
(
size_t
i
=
0
;
i
<
*
count
;
++
i
)
if
(
state
->
globals
.
names
[
i
]
==
index
)
assert
(
false
);
state
->
globals
.
names
[
*
count
]
=
index
;
++
(
*
count
);
string_write_byte
(
&
function
->
bc
,
0x0B
);
string_write_u16
(
&
function
->
bc
,
index
);
}
else
{
...
...
@@ -220,6 +259,13 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
// GLOBAL
u16
index
=
get_string_index
(
state
,
var
->
name
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
u16
*
count
=
&
state
->
globals
.
global_count
;
size_t
i
;
for
(
i
=
0
;
i
<
*
count
;
++
i
)
if
(
state
->
globals
.
names
[
i
]
==
index
)
break
;
if
(
i
==
*
count
)
assert
(
false
);
string_write_byte
(
&
function
->
bc
,
0x0C
);
string_write_u16
(
&
function
->
bc
,
index
);
}
else
{
...
...
@@ -240,6 +286,13 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
u16
index
=
get_string_index
(
state
,
assign
->
name
);
assert
(
constants
==
state
->
constants
.
constant_count
);
BCFunction
*
function
=
&
state
->
constants
.
constants
[
state
->
fp
];
u16
*
count
=
&
state
->
globals
.
global_count
;
size_t
i
;
for
(
i
=
0
;
i
<
*
count
;
++
i
)
if
(
state
->
globals
.
names
[
i
]
==
index
)
break
;
if
(
i
==
*
count
)
assert
(
false
);
string_write_byte
(
&
function
->
bc
,
0x0B
);
string_write_u16
(
&
function
->
bc
,
index
);
}
else
{
...
...
@@ -260,18 +313,30 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
String
generate_bc
(
Ast
*
ast
)
{
// init
String
bc
;
string_init
(
&
bc
);
BCCompilerState
bc_state
;
bc_state_init
(
&
bc_state
);
// generate header
// generate internals (constants (exp. functions), globals)
ast_to_bc
(
&
state
,
ast
);
ast_to_bc
(
&
bc_
state
,
ast
);
// header
string_write_byte
(
&
bc
,
'F'
);
string_write_byte
(
&
bc
,
'M'
);
string_write_byte
(
&
bc
,
'L'
);
string_write_byte
(
&
bc
,
'\0'
);
// constants to bc
for
(
size_t
i
=
0
;
i
<
bc_state
.
constants
.
constant_count
;
++
i
)
string_write_constant
(
&
bc
,
bc_state
.
constants
.
constants
[
i
]
);
// globals to bc
string_write_u16
(
&
bc
,
bc_state
.
globals
.
global_count
);
for
(
size_t
i
=
0
;
i
<
bc_state
.
globals
.
global_count
;
++
i
)
string_write_u16
(
&
bc
,
bc_state
.
globals
.
names
[
i
]
);
// EP
string_write_u16
(
&
bc
,
bc_state
.
ep
);
// free
bc_state_init_destroy
(
&
bc_state
);
return
bc
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/bc_compiler.h
+
2
−
1
View file @
4dfe740d
...
...
@@ -40,6 +40,7 @@ void string_destroy ( String * str );
void string_write_byte ( String * str, const u8 data );
void string_write_u16 ( String * str, const u16 data );
void string_write_i32 ( String * str, const i32 data );
void string_write_constant ( String * str, BCConstant constant );
typedef struct Constants {
BCConstant constants [ 256 * 256 ];
...
...
@@ -49,7 +50,7 @@ typedef struct Constants {
} Constants;
typedef struct Globals {
Str
names [ 256 * 256 ];
u16
names [ 256 * 256 ];
u16 global_count;
} Globals;
...
...
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