Skip to content

Commit

Permalink
feat: support floats & refactor playground to web assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
rabraghib committed Jun 6, 2024
1 parent 4c9274e commit 9218e5b
Show file tree
Hide file tree
Showing 16 changed files with 725 additions and 50 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ jobs:
cp bin/darijascript-bin-${{ needs.release-up.outputs.tag }}-linux bin/darijascript-bin
GOOS=windows GOARCH=amd64 go build -buildvcs=false -o bin/darijascript-bin-${{ needs.release-up.outputs.tag }}-windows.exe .
GOOS=darwin GOARCH=amd64 go build -buildvcs=false -o bin/darijascript-bin-${{ needs.release-up.outputs.tag }}-macos .
GOOS=js GOARCH=wasm go build -buildvcs=false -o bin/darijascript-bin-${{ needs.release-up.outputs.tag }}-web.wasm .
GOOS=js GOARCH=wasm go build -buildvcs=false -o website/public/darijascript-bin-web.wasm .
- uses: actions/upload-artifact@v4
with:
name: wasm-build
path: website/public/darijascript-bin-web.wasm
- name: Build vscode extension
run: |
npm install -g @vscode/vsce
Expand Down Expand Up @@ -70,6 +74,10 @@ jobs:
- uses: actions/checkout@v2
- name: Install Vercel CLI
run: npm install --global vercel@latest
- uses: actions/download-artifact@v4
with:
name: wasm-build
path: website/public/darijascript-bin-web.wasm
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
Expand Down
2 changes: 1 addition & 1 deletion cmd/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ You can type DarijaScript code and it will be evaluated immediately.`,
break
}
if sourceCode != "" {
runCode(sourceCode, eval)
RunCode(sourceCode, eval)
}
fmt.Print(">>> ")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Execute() {
func init() {
}

func runCode(sourceCode string, eval *interpreter.Evaluator) {
func RunCode(sourceCode string, eval *interpreter.Evaluator) {
l := lexer.NewLexer(string(sourceCode))
tokens := []*lexer.Token{}
for !l.IsEOL() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ darijascript run my_program.ds`,
os.Exit(1)
}
eval := interpreter.NewEvaluator()
runCode(string(sourceCode), eval)
RunCode(string(sourceCode), eval)
},
}

Expand Down
2 changes: 1 addition & 1 deletion examples/non_linear_eqq.ds
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fonksyon f(x) { rjje3 x*x-7; }
fonksyon df(x) { rjje3 2*x; }
fonksyon g(x) { rjje3 (3*x+7/x)/4; }

9ayed acceptableErr = 1/10000;
9ayed acceptableErr = 0.0001;

golih("Calculating the root of f(x) = x^3 - 2x - 5 using different methods:");
golih("1. Dicotomic method: " + dicothomy(2, 3, acceptableErr));
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import "github.com/rabraghib/darijascript/cmd"
import (
"github.com/rabraghib/darijascript/src/entrypoint"
)

func main() {
cmd.Execute()
entrypoint.Entrypoint()
}
9 changes: 9 additions & 0 deletions src/entrypoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !js

package entrypoint

import "github.com/rabraghib/darijascript/cmd"

func Entrypoint() {
cmd.Execute()
}
27 changes: 27 additions & 0 deletions src/entrypoint/main_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build js

package entrypoint

import (
"syscall/js"

"github.com/rabraghib/darijascript/cmd"
"github.com/rabraghib/darijascript/src/interpreter"
)

func Entrypoint() {
js.Global().Set("runDarijaScript", js.FuncOf(wasmRunner))

select {}
}

func wasmRunner(this js.Value, p []js.Value) interface{} {
if len(p) != 1 {
return "runDarijaScript() takes exactly 1 argument, " + string(len(p)) + " given"
}
sourceCode := p[0].String()
eval := interpreter.NewEvaluator()
cmd.RunCode(sourceCode, eval)

return nil
}
23 changes: 21 additions & 2 deletions src/interpreter/built_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ package interpreter

import (
"fmt"
"runtime"
"strconv"
"time"

"github.com/rabraghib/darijascript/src/parser"
)

func (eval *Evaluator) evaluateBuiltinFunctionCall(callExpression *parser.CallExpression) (interface{}, error) {
switch callExpression.Function.Value {
case "n3ess":
if len(callExpression.Arguments) != 1 {
return nil, fmt.Errorf("n3ess() takes exactly 1 argument, %d given", len(callExpression.Arguments))
}
arg, err := eval.evaluateExpression(callExpression.Arguments[0])
if err != nil {
return nil, err
}
num, err := convertToInt64(arg)
if err != nil {
return nil, err
}
time.Sleep(time.Duration(num) * time.Millisecond)
return nil, nil
case "abs":
if len(callExpression.Arguments) != 1 {
return nil, fmt.Errorf("abs() takes exactly 1 argument, %d given", len(callExpression.Arguments))
Expand All @@ -34,8 +50,11 @@ func (eval *Evaluator) evaluateBuiltinFunctionCall(callExpression *parser.CallEx
return nil, err
}
fmt.Println(arg)
return fmt.Sprintf("%d", arg), nil
return nil, nil
case "dakhel":
if runtime.GOOS == "js" {
return nil, fmt.Errorf("dakhel() is not supported in the browser")
}
if len(callExpression.Arguments) > 1 {
return nil, fmt.Errorf("dakhel() takes at most 1 argument, %d given", len(callExpression.Arguments))
}
Expand Down Expand Up @@ -116,7 +135,7 @@ func convertToString(value interface{}) (string, error) {
}
return "ghalt", nil
case float64:
return fmt.Sprintf("%f", v), nil
return strconv.FormatFloat(v, 'f', -1, 64), nil
case string:
return v, nil
default:
Expand Down
6 changes: 5 additions & 1 deletion src/lexer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func (l *Lexer) readChar() {

func (l *Lexer) readNumber() string {
position := l.position
for unicode.IsDigit(l.ch) {
usedDot := false
for isDigit(l.ch) || (l.ch == '.' && !usedDot) {
if l.ch == '.' {
usedDot = true
}
l.readChar()
}
return l.input[position:l.position]
Expand Down
2 changes: 2 additions & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

public/darijascript-bin-web.wasm
41 changes: 41 additions & 0 deletions website/app/LoadWasm/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import './wasm_exec.js';
import './wasmTypes.d.ts';

import React, { useEffect } from 'react';

async function loadWasm(writeOutput: (output: string) => void): Promise<void> {
const goWasm = new window.Go();
const result = await WebAssembly.instantiateStreaming(
fetch('darijascript-bin-web.wasm'),
goWasm.importObject
);

let outputBuf = '';
const decoder = new TextDecoder('utf-8');
window.fs.writeSync = function (fd, buf) {
const output = decoder.decode(buf);
writeOutput(output);
return buf.length;
};
goWasm.run(result.instance);
}

export const LoadWasm: React.FC<
React.PropsWithChildren<{
writeOutput: (output: string) => void;
}>
> = (props) => {
const [isLoading, setIsLoading] = React.useState(true);

useEffect(() => {
loadWasm(props.writeOutput).then(() => {
setIsLoading(false);
});
}, [props.writeOutput]);

// if (isLoading) {
// return <div>loading WebAssembly...</div>;
// } else {
// }
return <React.Fragment>{props.children}</React.Fragment>;
};
11 changes: 11 additions & 0 deletions website/app/LoadWasm/wasmTypes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare global {
export interface Window {
Go: any;
runDarijaScript: (code: string) => void;
fs: {
writeSync: (fd: number, buf: Uint8Array) => number;
};
}
}

export {};
Loading

0 comments on commit 9218e5b

Please sign in to comment.