Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
clean-and-itasks
sapl-interpreter
Commits
d2cf64e4
Commit
d2cf64e4
authored
Aug 03, 2015
by
Laszlo Domoszlai
Browse files
first stub of handling primitive functions
parent
ff83a578
Changes
7
Hide whitespace changes
Inline
Side-by-side
interpreter/compile.bat
View file @
d2cf64e4
g
++
-Wno-write-strings -Wno-pointer-arith
main
.c
desc
.c
mem
.c
parse
.c
-o
main
g
++
-Wno-write-strings -Wno-pointer-arith
main
.c
desc
.c
mem
.c
parse
.c
prim
.c
-o
main
interpreter/desc.c
View file @
d2cf64e4
...
...
@@ -43,6 +43,17 @@ FunEntry* alloc_prim(char* name)
return
entry
;
}
void
gen_slices
(
void
*
dest
,
void
*
forward_ptr
,
int
arity
)
{
for
(
int
i
=
0
;
i
<
arity
;
i
++
)
{
SliceEntry
*
slice
=
(
SliceEntry
*
)
dest
+
sizeof
(
SliceEntry
)
*
i
;
slice
->
flags
.
type
=
FT_SLICE
;
slice
->
flags
.
arity
=
i
;
slice
->
forward_ptr
=
forward_ptr
;
}
}
void
init_desc
()
{
__INT__
=
alloc_prim
(
"INT"
);
...
...
interpreter/desc.h
View file @
d2cf64e4
...
...
@@ -7,6 +7,7 @@
#define FT_CAF_REDUCED 4
#define FT_FUN 5
#define FT_SLICE 6
#define FT_PRIM 7
struct
FunFlags
{
...
...
@@ -58,6 +59,16 @@ struct RecordEntry
char
name
[];
};
struct
PrimEntry
{
struct
FunFlags
flags
;
int
strictness
;
void
(
*
exec
)();
char
name
[];
};
void
gen_slices
(
void
*
dest
,
void
*
forward_ptr
,
int
arity
);
void
init_desc
();
void
add_desc
(
char
*
fn
,
FunFlags
*
desc
);
...
...
interpreter/main.c
View file @
d2cf64e4
...
...
@@ -4,12 +4,14 @@
#include
"mem.h"
#include
"desc.h"
#include
"prim.h"
#include
"parse.h"
int
main
()
{
init_mem
();
init_desc
();
init_prim
();
char
*
stream
=
"40 R10 example._R2 1 9 example.a9 example.b37 F12 example.g_482 3 AF3 add2 VA0 VA1 39 C9 example.fAF12 example.g_482 LI1 LI2 55 F13 example.Start0 0 AF10 example._R2 LI1 VF9 example.f29 F4 main0 0 VF13 example.Start"
;
...
...
interpreter/parse.c
View file @
d2cf64e4
...
...
@@ -48,17 +48,6 @@ void copyStringAndForward(char* dest, char** source, int length)
*
source
+=
length
;
}
void
genSlices
(
void
*
dest
,
void
*
forward_ptr
,
int
arity
)
{
for
(
int
i
=
0
;
i
<
arity
;
i
++
)
{
SliceEntry
*
slice
=
(
SliceEntry
*
)
dest
+
sizeof
(
SliceEntry
)
*
i
;
slice
->
flags
.
type
=
FT_SLICE
;
slice
->
flags
.
arity
=
i
;
slice
->
forward_ptr
=
forward_ptr
;
}
}
int
parseDef1
(
char
**
ptr
)
{
int
defSize
;
...
...
@@ -106,7 +95,7 @@ int parseDef1(char** ptr)
entry
->
body
=
*
ptr
;
// generate slices. avoid function call if arity is zero
if
(
arity
>
0
)
gen
S
lices
(
entry_base
,
entry
,
arity
);
if
(
arity
>
0
)
gen
_s
lices
(
entry_base
,
entry
,
arity
);
add_desc
(
entry
->
name
,
(
FunFlags
*
)
entry
);
break
;
...
...
@@ -171,7 +160,7 @@ int parseDef1(char** ptr)
if
(
!
parseInt
(
ptr
,
&
entry
->
strictness
))
return
0
;
// generate slices. avoid function call if arity is zero
if
(
arity
>
0
)
gen
S
lices
(
entry_base
,
entry
,
arity
);
if
(
arity
>
0
)
gen
_s
lices
(
entry_base
,
entry
,
arity
);
add_desc
(
entry
->
name
,
(
FunFlags
*
)
entry
);
}
...
...
@@ -310,7 +299,7 @@ VarEntry* parseVar(char **ptr)
copyStringAndForward
(
name
,
ptr
,
nameLength
);
entry
->
f
=
(
FunFlags
*
)
find_desc
(
name
);
// can fail
if
(
entry
->
f
==
0
)
{
printf
(
"%s not found
\n
"
,
name
);
...
...
interpreter/prim.c
0 → 100644
View file @
d2cf64e4
#include
<string.h>
#include
"prim.h"
#include
"desc.h"
#include
"mem.h"
void
__add
()
{
}
PrimEntry
*
add_prim
(
int
arity
,
int
strictness
,
char
*
name
,
void
(
*
exec
)())
{
int
nameLength
=
strlen
(
name
);
// before the PrimEntry there are "arity" number of SliceEntries
void
*
entry_base
=
alloc_desc
(
sizeof
(
SliceEntry
)
*
arity
+
sizeof
(
PrimEntry
)
+
nameLength
+
1
);
PrimEntry
*
entry
=
(
PrimEntry
*
)
(
entry_base
+
sizeof
(
SliceEntry
)
*
arity
);
entry
->
flags
.
type
=
FT_PRIM
;
entry
->
flags
.
arity
=
arity
;
entry
->
strictness
=
strictness
;
entry
->
exec
=
exec
;
// TODO: should it be copied at all?
memcpy
(
entry
->
name
,
name
,
nameLength
);
entry
->
name
[
nameLength
]
=
'\0'
;
// generate slices. avoid function call if arity is zero
if
(
arity
>
0
)
gen_slices
(
entry_base
,
entry
,
arity
);
add_desc
(
entry
->
name
,
(
FunFlags
*
)
entry
);
}
void
init_prim
()
{
add_prim
(
2
,
3
,
"add"
,
&
__add
);
}
interpreter/prim.h
0 → 100644
View file @
d2cf64e4
#ifndef __PRIM_H
#define __PRIM_H
void
init_prim
();
#endif // __PRIM_H
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment