Skip to content

Commit 669f86d

Browse files
committed
FEAT: Modified the structure of the Rebol native code specification in C files
Now it looks more like the default Rebol source code header banner. This is full example of the native function including it's Rebol specification: ``` /*********************************************************************** ** */ REBNATIVE(sin) /* // sin: native [ // {Returns the trigonometric sine.} // value [decimal!] "In radians" // ] ***********************************************************************/ { SET_DECIMAL(D_RET, sin(VAL_DECIMAL(D_ARG(1)))); return R_RET; } ``` NOTE: these definitions are parsed in tools/make-headers.r script At this moment the parser don't work with C preprocessing - so it would collect also code from #ifndef blocks - this must be improved!
1 parent 722a9a9 commit 669f86d

8 files changed

+144
-88
lines changed

src/core/n-crypt.c

+24-25
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@
3535
const REBYTE rc4_name[] = "RC4-context"; //Used as a context handle name
3636
const REBYTE aes_name[] = "AES-context";
3737

38-
/**********************************************************************/
39-
//
40-
// rc4: native [
41-
//
42-
// "Encrypt/decrypt data (modifies) using RC4 algorithm."
38+
/***********************************************************************
39+
**
40+
*/ REBNATIVE(rc4)
41+
/*
42+
// rc4: native [
43+
// "Encrypt/decrypt data (modifies) using RC4 algorithm."
4344
//
44-
// /key "Provided only for the first time to get stream HANDLE!"
45-
// crypt-key [binary!] "Crypt key."
46-
// /stream
47-
// ctx [handle!] "Stream cipher context."
48-
// data [binary! none!] "Data to encrypt/decrypt."
49-
// ]
50-
REBNATIVE(rc4)
45+
// /key "Provided only for the first time to get stream HANDLE!"
46+
// crypt-key [binary!] "Crypt key."
47+
// /stream
48+
// ctx [handle!] "Stream cipher context."
49+
// data [binary! none!] "Data to encrypt/decrypt."
50+
// ]
51+
***********************************************************************/
5152
{
5253
REBOOL ref_key = D_REF(1);
5354
REBVAL *val_crypt_key = D_ARG(2);
@@ -86,23 +87,21 @@ REBNATIVE(rc4)
8687
return R_RET;
8788
}
8889

89-
90-
/**********************************************************************/
91-
//
90+
/***********************************************************************
91+
**
92+
*/ REBNATIVE(aes)
93+
/*
9294
// aes: native [
93-
//
94-
// "Encrypt/decrypt data using AES algorithm. Returns stream cipher context handle or encrypted/decrypted data."
95-
//
96-
// /key "Provided only for the first time to get stream HANDLE!."
97-
// crypt-key [binary!] "Crypt key (16 or 32 bytes)."
98-
// iv [none! binary!] "Optional initialization vector (16 bytes)."
95+
// "Encrypt/decrypt data using AES algorithm. Returns stream cipher context handle or encrypted/decrypted data."
96+
// /key "Provided only for the first time to get stream HANDLE!"
97+
// crypt-key [binary!] "Crypt key (16 or 32 bytes)."
98+
// iv [none! binary!] "Optional initialization vector (16 bytes)."
9999
// /decrypt "Use the crypt-key for decryption (default is to encrypt)"
100100
// /stream
101-
// ctx [handle!] "Stream cipher context."
102-
// data [binary!] "Data to encrypt/decrypt. Or NONE to close the cipher stream."
103-
101+
// ctx [handle!] "Stream cipher context."
102+
// data [binary!] "Data to encrypt/decrypt. Or NONE to close the cipher stream."
104103
// ]
105-
REBNATIVE(aes)
104+
***********************************************************************/
106105
{
107106
REBOOL ref_key = D_REF(1);
108107
REBVAL *val_crypt_key = D_ARG(2);

src/core/n-io.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -1048,16 +1048,18 @@ static REBSER *Read_All_File(char *fname)
10481048
return R_RET;
10491049
}
10501050

1051-
/**********************************************************************/
1052-
//
1053-
// access-os: native[
1054-
// {Access to various operating system functions (getuid, setuid, getpid, kill, etc.)}
1055-
// field [word!] "Valid words: uid, euid, gid, egid, pid"
1056-
// /set "To set or kill pid (sig 15)"
1057-
// value [integer! block!] "Argument, such as uid, gid, or pid (in which case, it could be a block with the signal no)"
1058-
// ]
1059-
//
1060-
REBNATIVE(access_os)
1051+
/***********************************************************************
1052+
**
1053+
*/ REBNATIVE(access_os)
1054+
/*
1055+
** access-os: native [
1056+
** {Access to various operating system functions (getuid, setuid, getpid, kill, etc.)}
1057+
** field [word!] "Valid words: uid, euid, gid, egid, pid"
1058+
** /set "To set or kill pid (sig 15)"
1059+
** value [integer! block!] "Argument, such as uid, gid, or pid (in which case, it could be a block with the signal no)"
1060+
** ]
1061+
**
1062+
***********************************************************************/
10611063
{
10621064
REBVAL *field = D_ARG(1);
10631065
REBOOL set = D_REF(2);

src/tools/common.r

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
REBOL [
2+
System: "REBOL [R3] Language Interpreter and Run-time Environment"
3+
Title: "Common make-* code"
4+
Rights: {
5+
Copyright 2012 REBOL Technologies
6+
REBOL is a trademark of REBOL Technologies
7+
}
8+
License: {
9+
Licensed under the Apache License, Version 2.0
10+
See: http://www.apache.org/licenses/LICENSE-2.0
11+
}
12+
Author: "Carl Sassenrath"
13+
]
14+
15+
do %form-header.r
16+
17+
;-- UTILITIES ----------------------------------------------------------
18+
19+
up-word: func [w] [
20+
w: uppercase form w
21+
foreach [f t] [
22+
#"-" #"_"
23+
][replace/all w f t]
24+
w
25+
]
26+
27+
to-c-name: func [word] [
28+
word: form word
29+
foreach [f t] [
30+
#"-" #"_"
31+
#"." #"_"
32+
#"?" #"q"
33+
#"!" #"x"
34+
#"~" ""
35+
#"*" "_p"
36+
#"+" "_add"
37+
#"|" "or_bar"
38+
][replace/all word f t]
39+
word
40+
]

src/tools/make-boot.r

+1-26
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ REBOL [
2121

2222
print "--- Make Boot : System Embedded Script ---"
2323

24-
do %form-header.r
24+
do %common.r
2525

2626
; Set platform TARGET
2727
do %systems.r
@@ -119,35 +119,10 @@ build: context [features: [help-strings]]
119119
;platform-data: platforms/:platform
120120
;build: platform-data/builds/:product
121121

122-
;-- UTILITIES ----------------------------------------------------------
123-
124-
up-word: func [w] [
125-
w: uppercase form w
126-
foreach [f t] [
127-
#"-" #"_"
128-
][replace/all w f t]
129-
w
130-
]
131-
132122
;-- Emit Function
133123
out: make string! 100000
134124
emit: func [data] [repend out data]
135125

136-
to-c-name: func [word] [
137-
word: form word
138-
foreach [f t] [
139-
#"-" #"_"
140-
#"." #"_"
141-
#"?" #"q"
142-
#"!" #"x"
143-
#"~" ""
144-
#"*" "_p"
145-
#"+" "_add"
146-
#"|" "or_bar"
147-
][replace/all word f t]
148-
word
149-
]
150-
151126
emit-enum: func [word] [emit [tab to-c-name word "," newline]]
152127

153128
emit-line: func [prefix word cmt /var /define /code /decl /up1 /local str][

src/tools/make-headers.r

+64-24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ REBOL [
1111
}
1212
Author: "Carl Sassenrath"
1313
Needs: 2.100.100
14+
History: [
15+
25-6-2018 "Oldes" {Added posibility to use `native` Rebol definition in C header}
16+
]
1417
]
1518

1619
print "------ Building headers"
@@ -24,7 +27,7 @@ chk-dups: true
2427
dups: make block! 10000 ; get pick [map! hash!] r3 1000
2528
dup-found: false
2629

27-
do %form-header.r
30+
do %common.r
2831
file-base: load %file-base.r
2932

3033
tmp: context [
@@ -42,7 +45,6 @@ insert base-code {REBOL [
4245
]
4346
}
4447

45-
4648
emit: func [d] [append repend output d newline]
4749
emit-rl: func [d] [append repend rlib d newline]
4850
emit-n: func [d] [append repend natives d newline]
@@ -87,6 +89,51 @@ append-spec: func [spec] [
8789
]
8890
]
8991

92+
ch_func-chars: charset [#"a" - #"z" #"A" - #"Z" "_!?-" #"0" - #"9"] ;just limited set!
93+
spec-reb: make string! 1024
94+
name: s: e: none
95+
96+
emit-native-spec: func[
97+
spec-c
98+
spec-rebol
99+
/local spec name
100+
][
101+
parse spec-c [
102+
thru "REBNATIVE(" copy name to ")"
103+
]
104+
105+
;trim leading chars from the rebol code block
106+
parse spec-rebol [
107+
["**" | " *" | "//"] (
108+
replace/all spec-rebol join "^/" (take/part spec-rebol 2) #"^/"
109+
)
110+
]
111+
112+
if any [
113+
error? try [spec: load spec-rebol]
114+
3 <> length? spec
115+
error? try [name: load name]
116+
not word? name
117+
(form name) <> (to-c-name first spec)
118+
][
119+
print ["^[[1;32;49m** In file: ^[[0m" the-file]
120+
print "^[[1;32;49m** Invalid NATIVE spec definition found: ^[[0m"
121+
print spec-rebol
122+
prin "^[[1;32;49m** For C spec:^[[0m^/^-"
123+
print spec-c
124+
ask "^/Press ENTER to continue."
125+
exit
126+
]
127+
128+
if c-file <> the-file [
129+
emit-n ["^/;-- " the-file]
130+
c-file: the-file
131+
]
132+
emit-n ["^/" name " {"]
133+
emit-n trim/head/tail detab spec-rebol
134+
emit-n #"}"
135+
]
136+
90137
func-header: [
91138
;-- Scan for function header box:
92139
"^/**" to newline
@@ -95,6 +142,15 @@ func-header: [
95142
newline
96143
[
97144
"/*" ; must be in func header section, not file banner
145+
[
146+
thru newline s:
147+
opt ["**" | " *" | "//"]
148+
some [#" " | #"^-"] some ch_func-chars #":" some [#" " | #"^-"] "native" any [#" " | #"^-"] #"[" thru newline
149+
to "^/****" e: (
150+
emit-native-spec spec copy/part s e
151+
)
152+
]
153+
|
98154
any [
99155
thru "**"
100156
[#" " | #"^-"]
@@ -106,37 +162,21 @@ func-header: [
106162
]
107163
]
108164

109-
ch_func-chars: charset [#"a" - #"z" #"A"]
110-
111-
spec-reb: make string! 1024
112-
name: none
113165

166+
;@@ Remove this rule later... let's use just the new style
114167
native-header: [
115168
;-- Scan for native header box:
116169
"^///" to newline (clear spec-reb)
117170
any [ "^///" copy line to newline (append append spec-reb line newline)]
118171
any [#"^/" | #" " | #"^-"]
119172
"REBNATIVE(" copy name to ")" (probe name
120-
either any [
121-
error? try [spec: load spec-reb]
122-
3 <> length? spec
123-
error? try [name: load name]
124-
not word? name
125-
][
126-
print "^[[1;32;49m** Invalid NATIVE spec definition found: ^[[0m"
127-
print spec-reb
128-
ask "Press ENTER to continue."
129-
][
130-
if c-file <> the-file [
131-
emit-n ["^/;-- " the-file]
132-
c-file: the-file
133-
]
134-
emit-n ["^/" name " {"]
135-
emit-n trim/head/tail detab spec-reb
136-
emit-n #"}"
137-
]
173+
print ["^[[1;32;49m** In file: ^[[0m" the-file]
174+
print "^[[1;32;49m** Found deprecated NATIVE spec definition: ^[[0m"
175+
print spec-reb
176+
ask "^/Press ENTER to continue."
138177
)
139178
]
179+
;@@-------------------------------------------------------
140180

141181
sym-chars: charset [#"A" - #"Z" #"_" #"0" - #"9"]
142182
sym-check: charset "/S"

src/tools/make-host-ext.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ REBOL [
2020
print "--- Make Host Boot Extension ---"
2121

2222
secure none
23-
do %form-header.r
23+
do %common.r
2424

2525
;-- Conversion to C strings, depending on compiler ---------------------------
2626

src/tools/make-os-ext.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ print ["--- Make OS Ext Lib --- Version:" lib-version]
2424
do %systems.r
2525
target: config-system/os-dir
2626

27-
do %form-header.r
27+
do %common.r
2828

2929
change-dir append %../os/ target
3030

src/tools/make-reb-lib.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ reb-ext-defs: out-dir/reb-lib-lib.h ; for REBOL usage
3131

3232
ver: load %../boot/version.r
3333

34-
do %form-header.r
34+
do %common.r
3535

3636
;-----------------------------------------------------------------------------
3737
;-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)