Skip to content

Commit

Permalink
added math.random; added GSX translations for native functions/values…
Browse files Browse the repository at this point in the history
…; updated GSX translations for keywords; bug fixes
  • Loading branch information
aName2050 authored Apr 26, 2024
1 parent 4d44d33 commit c26f50f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/GigaScript/gsx.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ setTokenData(TokenID.Class, NodeType.Class, 'students', OpPrec.None);
setTokenData(TokenID.Constructor, NodeType.Constructor, 'builder', OpPrec.None);
setTokenData(TokenID.Private, NodeType.Private, 'introvert', OpPrec.None);
setTokenData(TokenID.Public, NodeType.Public, 'extrovert', OpPrec.None);
setTokenData(TokenID.Static, NodeType.Static, 'unchanged', OpPrec.None);
setTokenData(TokenID.Static, NodeType.Static, 'unchanging', OpPrec.None);
setTokenData(TokenID.New, NodeType.New, 'new', OpPrec.None);
setTokenData(TokenID._This, NodeType.Identifier, 'this', OpPrec.None);

setTokenData(TokenID.If, NodeType.If, 'sus', OpPrec.None);
setTokenData(TokenID.Else, NodeType.Else, 'imposter', OpPrec.None);
setTokenData(TokenID.If, NodeType.If, 'shallItBe', OpPrec.None);
setTokenData(TokenID.Else, NodeType.Else, 'otherwise', OpPrec.None);

setTokenData(TokenID.While, NodeType.While, 'while', OpPrec.None);
setTokenData(TokenID.For, NodeType.For, 'for', OpPrec.None);
Expand All @@ -113,7 +113,7 @@ setTokenData(TokenID.Export, NodeType.Export, 'yeet', OpPrec.None);
setTokenData(TokenID.From, NodeType.From, 'from', OpPrec.None);
setTokenData(TokenID.As, NodeType.As, 'as', OpPrec.Assignment);

setTokenData(TokenID.Throw, NodeType.Throw, '💀', OpPrec.None);
setTokenData(TokenID.Throw, NodeType.Throw, 'shoot', OpPrec.None);

setTokenData(TokenID.Try, NodeType.Try, 'messAround', OpPrec.None);
setTokenData(TokenID.Catch, NodeType.Catch, 'findOut', OpPrec.None);
Expand Down Expand Up @@ -151,7 +151,7 @@ setTokenData(

// punctation
setTokenData(TokenID.Semicolon, NodeType.Semicolon, 'rn', OpPrec.None);
setTokenData(TokenID.Colon, NodeType.Colon, ':', OpPrec.None);
setTokenData(TokenID.Colon, NodeType.Colon, 'is', OpPrec.None);
setTokenData(TokenID.Dot, NodeType.Dot, '.', OpPrec.None);
setTokenData(TokenID.Comma, NodeType.Comma, ',', OpPrec.None);

Expand Down
13 changes: 12 additions & 1 deletion src/GigaScript/native/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataConstructors, Value } from '../runtime/types';
import { DataConstructors, GSNumber, Value } from '../runtime/types';
import { getValue } from '../util/getValue';

export const print = DataConstructors.NATIVEFN((args, scope) => {
Expand Down Expand Up @@ -28,6 +28,17 @@ export const math = DataConstructors.OBJECT(
return DataConstructors.NUMBER(Math.sqrt(num));
})
)
.set(
'random',
DataConstructors.NATIVEFN((args, _scope) => {
const max = (args[0] as GSNumber) || DataConstructors.NUMBER(1);
const min = (args[1] as GSNumber) || DataConstructors.NUMBER(0);

return DataConstructors.NUMBER(
Math.random() * (max.value - min.value) + min.value
);
})
)
);

export const formatString = DataConstructors.NATIVEFN((args, scope) => {
Expand Down
2 changes: 2 additions & 0 deletions src/GigaScript/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ export default class Parser {
this.current().type == NodeType.Public ||
this.current().type == NodeType.Private
) {
// property notation
// <public | private> <static?> [IDENTIFIER] = [VALUE];
const startPos = this.current().__GSC._POS;
const isPublic = this.advance().type == NodeType.Public;

Expand Down
21 changes: 14 additions & 7 deletions src/GigaScript/runtime/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,34 @@ import {
} from '../ast/class.ast';
import { evaluate } from './interpreter/interpreter';
import { STATEMENT } from '../ast/ast';
import { sourceFile } from '../..';

export function createGlobalScope(cwd: string): Environment {
const env = new Environment(cwd);

const isGSXFile = sourceFile?.endsWith('.gsx');

// Native values
env.declareVar('true', NativeValues.True, true);
env.declareVar('false', NativeValues.False, true);
env.declareVar('null', NativeValues.Null, true);
env.declareVar('undefined', NativeValues.Undefined, true);
env.declareVar(isGSXFile ? 'nocap' : 'true', NativeValues.True, true);
env.declareVar(isGSXFile ? 'cap' : 'false', NativeValues.False, true);
env.declareVar(isGSXFile ? 'fake' : 'null', NativeValues.Null, true);
env.declareVar(
isGSXFile ? 'undefined' : 'undefined',
NativeValues.Undefined,
true
);

// Native variables
env.declareVar('error', NativeValues.Error, true);
env.declareVar(isGSXFile ? 'whoops' : 'error', NativeValues.Error, true);

// Native functions
env.declareVar('print', NativeFunctions.print, true);
env.declareVar(isGSXFile ? 'yap' : 'print', NativeFunctions.print, true);
env.declareVar(
'generateTimestamp',
NativeFunctions.generateTimestamp,
true
);
env.declareVar('math', NativeFunctions.math, true);
env.declareVar(isGSXFile ? 'nerd' : 'math', NativeFunctions.math, true);
env.declareVar('formatString', NativeFunctions.formatString, true);

return env;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ if (debug) {
}

if (useCUDA) {
throw 'Tokenization using NVIDIA(R) CUDA(R) Cores';
throw 'Tokenization using NVIDIA(R) CUDA(R) Cores is currently not available';
}

const fileLocation: string = file ? path.parse(file).dir : '';
Expand Down
12 changes: 6 additions & 6 deletions tests/test.gsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
lit mutable be "hello" rn
bro constant be "yoo" rn
lit truthy be nocap rn
lit falsey be cap rn

print(mutable)
print(constant)
yap(truthy)
yap(falsey)

mutable be 'changed'
"shoot 'error!!'"

print(mutable)
yap(nerd.random(1, 10))

0 comments on commit c26f50f

Please sign in to comment.