Skip to content

Commit d24ce1f

Browse files
committed
fix bugs
1 parent f85f3de commit d24ce1f

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

packages/hedgehog-core/src/transpiler/preprocessor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ using demo_function
106106

107107

108108

109-
// code is the string of code, and strCurrentCallStack is the full call stack at current process
110-
// for example
109+
// code is the string of code, and strCurrentCallStack is the full call stack
111110
async function preprocessDFS(code: string, strCurrentCallStack: string): Promise<string> {
112111
//1. split the codes into lines
113112
let vecSplittedString = code.split(/\r?\n/);
@@ -118,20 +117,22 @@ async function preprocessDFS(code: string, strCurrentCallStack: string): Promise
118117
//3. process each line of code
119118
try{
120119
for (let i=0;i<vecSplittedString.length; i++){
120+
returnCode += '\n';
121121
//3.1 if current line of code doesn't contain "*import ", just append it to returnCode
122122
if (!vecSplittedString[i].includes("*import ")){ returnCode += '\n' + vecSplittedString[i]; }
123123
//3.2 otherwise, split the string by "*import ", keep the first part (if it exists), then download
124124
// and fetch the second part recursively (which should be and must be a valid URL)
125125
else {
126126
let currentString = vecSplittedString[i];
127127
let splittedResult = currentString.split("*import ");
128-
if (splittedResult.length!=2) {
128+
if (splittedResult.length<2) {
129129
throw "Invalid current line of code for preprocessing: \n"
130130
+ "\nCall stack: \n" + strCurrentCallStack
131131
+ "\nCurrent line: "+ currentString + "\n";
132132
}
133133
//3.2.1 add the first part
134134
returnCode += splittedResult[0];
135+
console.log(splittedResult[0] + " is added to source code" )
135136
//3.2.2 download the library from URL
136137
let libraryFromUrl = await fetch(splittedResult[1], { method: 'get' })
137138
.then(function (body) {

packages/hedgehog-lab/src/tutorials.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,94 @@ This document is created and maintained by Hedgehog Lab Community. The markdown
290290
\`)
291291
292292
293+
`;
294+
295+
296+
const moduleSource = `
297+
/*
298+
File: fibonacci.hs
299+
Location: https://gist.githubusercontent.com/lidangzzz/86c78163bf7838220224530d6e36aec9/raw/da89c75d4b6671dc0936240a62d483bf67e2b9ef/fibonacci.hs
300+
301+
function fibonacci(x){
302+
if (x<0) return 0;
303+
if (x==1 || x==0) return 1;
304+
305+
//elst x>=2
306+
let dp = [1,1]
307+
for (let i=2;i<=x;i++){ let val = dp[dp.length-1] + dp[dp.length-2]; dp.push(val)}
308+
return dp[x];
309+
}
310+
*/
311+
312+
313+
// 1. import the function from URL...
314+
*import https://gist.githubusercontent.com/lidangzzz/86c78163bf7838220224530d6e36aec9/raw/da89c75d4b6671dc0936240a62d483bf67e2b9ef/fibonacci.hs
315+
316+
317+
// 2. then use it.
318+
for (let i=0;i<10;i++) {
319+
print("Fibonacci with index " + i + ": " + fibonacci(i) );
320+
}
321+
322+
//3. or import as a local function/class/variable
323+
myFibonacci = *import https://gist.githubusercontent.com/lidangzzz/86c78163bf7838220224530d6e36aec9/raw/da89c75d4b6671dc0936240a62d483bf67e2b9ef/fibonacci.hs
324+
325+
for (let i=0;i<10;i++) {
326+
print("myFibonacci with index " + i + ": " + myFibonacci(i));
327+
}
328+
329+
/*
330+
Comments:
331+
332+
1. The fibonacci.hs file located at follow url will be imported
333+
automatically from the preprocessor by replacing the "import URL"
334+
part with the big chunk of raw string from the file into your script
335+
(just like C++ macro #include<your_header.h>).
336+
337+
2. If you are a module/library/class/function developer, please make sure
338+
that every function/class/variable is imported inside your scope, for example:
339+
--
340+
my_function.hs
341+
342+
function my_function(x,y,z){
343+
let f1 = * import http://myWebsite.com/f1.hs
344+
let f2 = * import http://myWebsite.com/f2.hs
345+
let val = f1(x) + f2(y) + z*z;
346+
return val;
347+
}
348+
--
349+
which will guarantee that all imported functions/classes (f1 and f2)
350+
are defined and used only in your scope of "myfunction". This won't
351+
pollute any functions/classes/variables out of your scope.
352+
353+
3. If you are a teacher or researcher sharing a piece of code with others,
354+
then it's a good choice that includes all necessary functions/classes/variables
355+
into a single hs file so that other people can import once and have all setup
356+
ready, for example
357+
--
358+
environment_setup.hs
359+
360+
let dataset_1 = getDataset1();
361+
let dataset_2 = getDataset2();
362+
function regression(x,y){...}
363+
class MyClass1{ ... }
364+
let ground_truth = [1,1,1,0,0,...]
365+
let test_dataset = getTestDataset();
366+
--
367+
368+
Then other people can includes all above by adding one line of code at beginning
369+
* import http://..../environment_setup.hs
370+
371+
doSomething(dataset_1);
372+
draw(regression(dataset_2));
373+
...
374+
375+
4. Do NOT put space between * and "import". Also even if *"import" is used in comments,
376+
it will still be used as a macro & string replacement. So do not use in comments at
377+
this version.
378+
*/
379+
380+
293381
`;
294382

295383
export const tutorials = [
@@ -324,5 +412,9 @@ export const tutorials = [
324412
{
325413
description: 'Markdown',
326414
source: markdownSource
415+
},
416+
{
417+
description: 'Module',
418+
source: moduleSource,
327419
}
328420
];

0 commit comments

Comments
 (0)