Skip to content

Commit 7d6951f

Browse files
authored
Add some basic files in here.
macro is the main file in here. You could use its release to use Javascript macro definition.
0 parents  commit 7d6951f

File tree

7 files changed

+302
-0
lines changed

7 files changed

+302
-0
lines changed

.readme.md.un~

637 Bytes
Binary file not shown.

example/example.jc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#define doc document
2+
#define get_id getElementById
3+
#define get_class GetElementByClass
4+
#define fun function
5+
#define auto var
6+
#nodef hi
7+
#define class doc.get_class
8+
fun hi(){
9+
auto i=doc.get_id('Hello');
10+
auto b=class('Hi');
11+
return i;
12+
}

example/example.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#nodef hi
2+
function hi(){
3+
var i=document.getElementById('Hello');
4+
var b=document.GetElementByClass('Hi');
5+
return i;
6+
}
7+

readme.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
### JavaScript macro definition enable
2+
Current program is for convert macro definiton use in JavaScript.
3+
For example:
4+
~~~C
5+
#define doc document
6+
#define get_id getElementById
7+
#define get_class GetElementByClass
8+
#define fun function
9+
#define auto var
10+
#nodef hi
11+
#define class doc.get_class
12+
fun hi(){
13+
auto i=doc.get_id('Hello');
14+
auto b=class('Hi');
15+
return i;
16+
}
17+
~~~
18+
coule be convert to :
19+
20+
~~~JavasScript
21+
#nodef hi
22+
function hi(){
23+
var i=document.getElementById('Hello');
24+
var b=document.GetElementByClass('Hi');
25+
return i;
26+
}
27+
~~~
28+
29+
Support:
30+
- single macro definition
31+
- nested macro definition
32+
33+
Usage:
34+
macro.exe wait_convert_file_name . . . wait_convert_file_name
35+
36+
Notice: You should write you file append .jc rear. Then the final resulat will be .js format. Otherwise the convert ccontent will be added to the original file.! ! !

readme.md~

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
### JavaScript macro definition enable
2+
Current program is for convert macro definiton use in JavaScript.
3+
For example:
4+
~~~C
5+
#define doc document
6+
#define get_id getElementById
7+
#define get_class GetElementByClass
8+
#define fun function
9+
#define auto var
10+
#nodef hi
11+
#define class doc.get_class
12+
fun hi(){
13+
auto i=doc.get_id('Hello');
14+
auto b=class('Hi');
15+
return i;
16+
}
17+
~~~
18+
coule be convert to :
19+
20+
~~~JavasScript
21+
#nodef hi
22+
function hi(){
23+
var i=document.getElementById('Hello');
24+
var b=document.GetElementByClass('Hi');
25+
return i;
26+
}
27+
~~~
28+
29+
Support:
30+
- single macro definition
31+
- nested macro definition
32+
33+
Usage:
34+
macro wait_convert_file_name . . . wait_convert_file_name
35+
36+
Notice: You should write you file append .jc rear. Then the final resulat will be .js format. Otherwise the convert ccontent will be added to the original file.! ! !

release/macro.exe

18 KB
Binary file not shown.

src/macro.c

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#define flags 1
4+
#define false 0
5+
//#define test
6+
char cmp(const char *a, const char *b)
7+
{
8+
if(*a!=*b)
9+
return false;
10+
static const char *re;
11+
re=b;
12+
while(*++a&&*++b)
13+
if(*a!=*b)
14+
return false;
15+
if(!*a)
16+
b++;
17+
if(*a|| *a=='\0'&&*b=='\0' )
18+
return b-re;
19+
else return false;
20+
}
21+
char len(const char *s)
22+
{
23+
char i=0;
24+
while(*s++)
25+
i++;
26+
return i;
27+
}
28+
29+
char * strsub(char *a,const char *sear,const char *sub)
30+
{
31+
static char *va;
32+
va=a;
33+
34+
char re,ti=0;
35+
while(*a)
36+
{
37+
if(*a==*sear)
38+
{
39+
re=cmp(a,sear);
40+
if(re)
41+
{
42+
*a=flags;
43+
ti++;
44+
}
45+
}
46+
a++;
47+
}
48+
#ifdef test
49+
printf("Original data:%s-%d \n",va,a-va);
50+
printf("search data:%s -%d \n",sear,len(sear));
51+
printf("Substitle data:%s -%d\n",sub,len(sub));
52+
#endif
53+
int length=(a-va+1)+ti*(len(sub)-len(sear));
54+
#ifdef test
55+
printf("ti:%d\nStack: %d\n",ti,length);
56+
#endif
57+
char *r;
58+
r=(char *)malloc(length);
59+
a=r;
60+
const char * res;
61+
re=len(sear);
62+
while(*va)
63+
if(*va!=flags)
64+
*a++=*va++;
65+
else
66+
{
67+
res=sub;
68+
while(*res)
69+
*a++=*res++;
70+
va+=re;
71+
}
72+
*a='\0';
73+
return r;
74+
}
75+
void get_line(char *s)
76+
{
77+
char c;
78+
while((c=getchar())!='\n')
79+
*s++=c;
80+
#ifndef test
81+
*s++='\n';
82+
#endif
83+
*s='\0';
84+
}
85+
int main(int argc, char *argv[])
86+
{
87+
char i;
88+
#ifdef test
89+
char space[80];
90+
char search[80];
91+
char subt[80];
92+
#endif
93+
int file_length;
94+
FILE *in,*out;
95+
char *pace,*cc,*sear,*sub,*file_pace,*ss,*cle;
96+
//puts(argv[1]);
97+
98+
for(i=1;i<argc;i++)
99+
{
100+
in=fopen(argv[i],"r");
101+
if(in==NULL)
102+
{
103+
printf("Can't open %s\n.",argv[1]);
104+
continue;
105+
}
106+
107+
cc=strsub(argv[i],".jc",".js");
108+
out=fopen(cc,"a");
109+
puts(cc);
110+
free(cc);
111+
112+
fseek(in,0,SEEK_END);
113+
file_length=ftell(in);
114+
pace=(char *)malloc(file_length);
115+
if(pace==NULL)
116+
{
117+
puts("Sorry for spare memory.");
118+
return 0;
119+
}
120+
121+
fseek(in,0,SEEK_SET);
122+
fread(pace,file_length,1,in);
123+
// puts("pace normal");
124+
fclose(in);
125+
126+
127+
pace[file_length-1]='\0';
128+
//puts(pace); -m32 mode file read more than itself
129+
cc=pace;
130+
131+
file_pace=(char*)malloc(file_length);
132+
ss=file_pace;
133+
// puts("file_pace normal");
134+
135+
cle=pace;
136+
137+
char record=0;
138+
while(*cc)
139+
{
140+
if(record)
141+
{
142+
*ss++='#';
143+
record=0;
144+
}
145+
while(*cc!='\0'&&*cc!='#')
146+
*ss++=*cc++;
147+
if(!*cc)
148+
break;
149+
if(*cc='#')
150+
{
151+
record=1;
152+
cc++;
153+
}
154+
if(cmp(cc,"define")==0)
155+
continue;
156+
else
157+
record=0;
158+
//puts("I am here");
159+
while(*cc!=' ')// clear define
160+
cc++;
161+
162+
while(*cc==' ')
163+
cc++;
164+
sear=cc;
165+
while(*cc!=' ')
166+
cc++;
167+
*cc='\0';
168+
169+
while(*++cc==' ')
170+
continue;
171+
sub=cc;
172+
while(*++cc!=' '&&*cc!='\n')
173+
continue;
174+
*cc='\0';
175+
176+
cc++;
177+
//puts("I come again");
178+
cc=strsub(cc,sear,sub);
179+
//puts("May I here?");
180+
free(cle);
181+
cle=cc;
182+
}
183+
// puts(cle);
184+
free(cle);
185+
// *ss='\0';
186+
// puts(file_pace);
187+
*ss=' ';
188+
fwrite(file_pace,ss-file_pace+1,1,out);
189+
// while(*--ss!='\n')
190+
// putchar(*ss);
191+
free(file_pace);
192+
fclose(out);
193+
}
194+
return 0;
195+
}
196+
197+
#ifdef test
198+
printf("Cmp:%d\n",cmp(".jc",".jc"));
199+
char *s;
200+
while(1)
201+
{
202+
get_line(space);
203+
get_line(search);
204+
get_line(subt);
205+
s=strsub(space,search,subt);
206+
puts(s);
207+
free(s);
208+
}
209+
}
210+
#endif
211+

0 commit comments

Comments
 (0)