about summary refs log tree commit diff stats
path: root/js/scripting-lang/parser.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/parser.js')
-rw-r--r--js/scripting-lang/parser.js48
1 files changed, 28 insertions, 20 deletions
diff --git a/js/scripting-lang/parser.js b/js/scripting-lang/parser.js
index b94696a..a5cb45b 100644
--- a/js/scripting-lang/parser.js
+++ b/js/scripting-lang/parser.js
@@ -4,6 +4,14 @@
 
 import { TokenType } from './lexer.js';
 
+// Cross-platform environment detection
+const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
+const isBun = typeof process !== 'undefined' && process.versions && process.versions.bun;
+const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
+
+// Cross-platform debug flag
+const DEBUG = (isNode && process.env.DEBUG) || (isBrowser && window.DEBUG) || false;
+
 /**
  * AST node types for the language
  * 
@@ -334,7 +342,7 @@ export function parser(tokens) {
      * patterns, and unexpected tokens during pattern parsing.
      */
     function parseWhenExpression() {
-        if (process.env.DEBUG) {
+        if (DEBUG) {
             console.log(`[DEBUG] parseWhenExpression: starting, current token = ${tokens[current].type}`);
         }
         current++; // Skip 'when'
@@ -365,7 +373,7 @@ export function parser(tokens) {
         const cases = [];
         
         while (current < tokens.length) {
-            if (process.env.DEBUG) {
+            if (DEBUG) {
                 console.log(`[DEBUG] parseWhenExpression: starting new case, current token = ${tokens[current].type}, value = ${tokens[current].value || 'N/A'}`);
             }
             // Parse pattern(s) - can be single pattern or multiple patterns
@@ -374,7 +382,7 @@ export function parser(tokens) {
             // Parse patterns until we hit THEN
             while (current < tokens.length && tokens[current].type !== TokenType.THEN) {
                 let pattern;
-                if (process.env.DEBUG) {
+                if (DEBUG) {
                     console.log(`[DEBUG] parseWhenExpression: parsing pattern, current token = ${tokens[current].type}, value = ${tokens[current].value || 'N/A'}`);
                 }
                 
@@ -524,7 +532,7 @@ export function parser(tokens) {
                 result: [result]
             });
             
-            if (process.env.DEBUG) {
+            if (DEBUG) {
                 console.log(`[DEBUG] parseWhenExpression: finished case, current token = ${tokens[current].type}, value = ${tokens[current].value || 'N/A'}`);
             }
             
@@ -532,13 +540,13 @@ export function parser(tokens) {
             if (current < tokens.length) {
                 const nextToken = tokens[current];
                 
-                if (process.env.DEBUG) {
+                if (DEBUG) {
                     console.log(`[DEBUG] parseWhenExpression: checking termination, nextToken = ${nextToken.type}, value = ${nextToken.value || 'N/A'}`);
                 }
                 
                 // Stop on semicolon
                 if (nextToken.type === TokenType.SEMICOLON) {
-                    if (process.env.DEBUG) {
+                    if (DEBUG) {
                         console.log(`[DEBUG] parseWhenExpression: terminating on SEMICOLON`);
                     }
                     current++;
@@ -547,7 +555,7 @@ export function parser(tokens) {
                 
                 // Stop on assignment (for consecutive assignments)
                 if (nextToken.type === TokenType.ASSIGNMENT) {
-                    if (process.env.DEBUG) {
+                    if (DEBUG) {
                         console.log(`[DEBUG] parseWhenExpression: terminating on ASSIGNMENT`);
                     }
                     break;
@@ -566,7 +574,7 @@ export function parser(tokens) {
                     
                     if (lookAhead < tokens.length && tokens[lookAhead].type === TokenType.ASSIGNMENT) {
                         // This is the start of a new assignment, terminate the when expression
-                        if (process.env.DEBUG) {
+                        if (DEBUG) {
                             console.log(`[DEBUG] parseWhenExpression: terminating on new assignment starting with ${nextToken.value}`);
                         }
                         break;
@@ -575,7 +583,7 @@ export function parser(tokens) {
                 
                 // Stop on right brace (for when expressions inside table literals)
                 if (nextToken.type === TokenType.RIGHT_BRACE) {
-                    if (process.env.DEBUG) {
+                    if (DEBUG) {
                         console.log(`[DEBUG] parseWhenExpression: terminating on RIGHT_BRACE`);
                     }
                     break;
@@ -583,7 +591,7 @@ export function parser(tokens) {
                 
                 // Stop on comma (for when expressions inside table literals)
                 if (nextToken.type === TokenType.COMMA) {
-                    if (process.env.DEBUG) {
+                    if (DEBUG) {
                         console.log(`[DEBUG] parseWhenExpression: terminating on COMMA`);
                     }
                     break;
@@ -885,7 +893,7 @@ export function parser(tokens) {
      * Error handling includes checks for missing operators or operands.
      */
     function parseExpression() {
-        if (process.env.DEBUG) {
+        if (DEBUG) {
             console.log(`[DEBUG] parseExpression: starting, current token = ${tokens[current].type}`);
         }
         
@@ -903,7 +911,7 @@ export function parser(tokens) {
         // Handle unary minus at the beginning of expressions
         let left;
         if (current < tokens.length && (tokens[current].type === TokenType.MINUS || tokens[current].type === TokenType.UNARY_MINUS)) {
-            if (process.env.DEBUG) {
+            if (DEBUG) {
                 console.log(`[DEBUG] parseExpression: handling unary minus`);
             }
             current++;
@@ -917,14 +925,14 @@ export function parser(tokens) {
             left = parseTerm();
         }
         
-        if (process.env.DEBUG) {
+        if (DEBUG) {
             console.log(`[DEBUG] parseExpression: after parseTerm, current token = ${tokens[current].type}`);
         }
         
         while (current < tokens.length) {
             const token = tokens[current];
             
-            if (process.env.DEBUG) {
+            if (DEBUG) {
                 console.log(`[DEBUG] parseExpression: while loop, current token = ${token.type}, value = ${token.value || 'N/A'}`);
             }
             
@@ -988,7 +996,7 @@ export function parser(tokens) {
      * Error handling includes checks for missing operators or operands.
      */
     function parseTerm() {
-        if (process.env.DEBUG) {
+        if (DEBUG) {
             console.log(`[DEBUG] parseTerm: starting, current token = ${tokens[current].type}`);
         }
         let left = parseApplication();
@@ -1041,7 +1049,7 @@ export function parser(tokens) {
      * Error handling includes checks for missing operators or operands.
      */
     function parseFactor() {
-        if (process.env.DEBUG) {
+        if (DEBUG) {
             console.log(`[DEBUG] parseFactor: starting, current token = ${tokens[current].type}`);
         }
         let left = parsePrimary();
@@ -1480,7 +1488,7 @@ export function parser(tokens) {
             throw new Error('Unexpected end of input');
         }
         
-        if (process.env.DEBUG) {
+        if (DEBUG) {
             console.log(`[DEBUG] parsePrimary: current token = ${token.type}, value = ${token.value || 'N/A'}`);
         }
         
@@ -1626,9 +1634,9 @@ export function parser(tokens) {
 
             case TokenType.LEFT_PAREN:
                 current++;
-                if (process.env.DEBUG) {
-                    console.log(`[DEBUG] parsePrimary: parsing LEFT_PAREN, current token = ${tokens[current].type}`);
-                }
+                            if (DEBUG) {
+                console.log(`[DEBUG] parsePrimary: parsing LEFT_PAREN, current token = ${tokens[current].type}`);
+            }
                 const expression = parseLogicalExpression();
                 if (current >= tokens.length || tokens[current].type !== TokenType.RIGHT_PAREN) {
                     throw new Error('Expected ")" after expression');