Here is my language code.
I am a native Hispanic, so the labels and some parts are in Spanish, but the rest of the code, according to me, is in English.
What I want to do is create a function-oriented programming language in pegjs.
The language aims to create video game engines; graphics engines; web pages and video games.
I am creating the language as a personal challenge and also to have something to do
The error “ReferenceError: expression is not defined” indicates that you are trying to use the variable expression in a place where it is not defined or has not been previously declared in the code although, I have already tried to put it after having declared expression but it still does not work.
Help me please.
start
= variableLoc / variableVarion / variableConst / variableEntro / variableDal / variableTex / declaracionFunciones / llamarFuncion / login /ingre / ingreNum /ingretro / ingreDal
//funciones
declaracionFunciones =
"func" space id:id "(" parameterList:parameterList? ")" "{" statement:statement* "}" {
return { type: "declaracionFunciones", id, parameterList:parameterList || [], statement };
}
llamarFuncion =
id:id "(" parameterList:parameterList? ")" ";" {
return { type: "llamarFuncion", id, parameterList: parameterList || [] };
}
login
= "login" space "(" target:id "," value:expression "," optionalTarget:id? ")" ";" {
return { type: "login", target, value, optionalTarget: optionalTarget || null };
}
ingre = "ingre" space? "(" target:id ")" ";" {
variable[target] = prompt(target);
return { type: "ingre", target, value: variable[target] };
}
ingreNum = "ingreNum" space? "(" target:id ")" ";" {
Number.isFloat = Number.isFloat || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) !== value;
};
let inputValue = prompt(target);
let num = prompt(target);
let parsedValue = Number(inputValue);
if (Number.isInteger(parsedValue)) {
variable[target] = parseInt(num);
} else if (Number.isFloat(parsedValue)) {
variable[target] = parseFloat(num);
} else {
try {
throw new Error("Sintax error");
} catch(error) {
console.error(error.message);
}
}
return { type: "ingreNum", target, value: variable[target] };
};
ingretro = "ingretro" space? "("target:id")" ";" {
variable[target] = parseInt(prompt(target));
return { type: "ingretro", target, value: variable[target] };
};
ingreDal = "ingreDal" space? "("target:id")" ";" {
Number.isFloat = Number.isFloat || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) !== value;
};
variable[target] = parseFloat(prompt(target));
return { type: "ingretro", target, value: variable[target] };
};
//DeclaraciĆ³n de variables
variableLoc =
"loc" space id:id (space? "=" space? expression:expression)? ";" {
return { type: "variableLoc", id, expression:expression, isLocal: true };
}
variableVarion =
"varion" space id:id (space? "=" space? expression: expression)? ";" {
return { type: "variableVarion", id, expression, isGlobal: true };
}
variableConst =
"const" space id:id (space? "=" space? expression)? ";" {
return { type: "variableConst", id, expression, isConstant: true, isLocal: true };
}
variableTex =
"tex" space id:id (space? "=" space? string:string)? ";" {
return { type: "variableTex", id, string:string, isLocal: true};
}
variableEntro =
"entro" space id:id (space? "=" space? numberEntro:numberEntro)? ";" {
return { type: "variableEntro", id, numberEntro:numberEntro, isLocal: true };
}
variableDal =
"dal" space id:id (space? "=" space? numberDal:numberDal)? ";" {
return { type:"variableDal", id, numberDal:numberDal, isLocal: true };
}
//Reglas de declaraciĆ³n de variable
id = [a-zA-Z_][a-zA-Z0-9_]* {
return text();
}
expression = number / string / boolean / id
//Numeros
number = integer / float / negativeInteger / negativeFloat / specialValue
numberEntro = integer / negativeInteger
numberDal = float / negativeFloat
integer = [0-9]+ {
return parseInt(text(), 10);
}
float = [0-9]+ "." [0-9]+ {
return parseFloat(text(), 10);
}
negativeInteger = "-" integer {
return -parseInt(text(), 10);
}
negativeFloat = "-" float {
return -parseFloat(text(), 10);
}
specialValue = "NaN" {
return { type: "specialValue", value: NaN };
}
/ "INF" {
return { type: "specialValue", value: Infinity };
}
/ "-INF" {
return { type: "specialValue", value: -Infinity };
}
//Strings
string = stringDouble / stringSingle / stringTemplate / stringTemplateContent
stringDouble = '"' chars:stringContent* '"' {
return chars.join('');
}
stringSingle = "'" chars:(stringContent / "\'")* "'" {
return chars.join('');
}
stringTemplate = "`" chars:stringTemplateContent* "`" {
return chars.join('');
}
stringTemplateContent = "${" expression:expression "}" {
return "${" + expression + "}";
}
/ [^$]+
stringContent = [^"] / "\" . ;
//Booleanos
boolean = "true" / "false" {
return text() === "true";
}
//Reglas de funciones
parameterList
= parameter (space "," space parameter)*
parameter
= expression:expression space id:id {
return { type: "parameter", expression:expression, name: id };
}
outputStatement
= "consola" ":" expression:expression ";" {
return { type: "outputStatement", target: "consola", value: expression };
}
/ "documento" ":" expression:expression ";" {
return { type: "outputStatement", target: "documento", value: expression };
}
statement
= outputStatement/ login / expression / assignment / returnStatement
assignment
= id:id space "=" space expression:expression ";" {
return { type: "assignment", variable: id, value: expression };
}
returnStatement
= "regresa" space expression:expression ";" {
return { type: "returnStatement", value: expression };
}
//Reglas generales
space = " "*
variable = statement:statement {
return { variables: {}, statement };
}
I was hoping to be able to do:
varion a;
a = 10;
login("consola",a);
ReferenceError: expression is not defined