RosettaCodeData/Task/Extend-your-language/XBS/extend-your-language.xbs
2023-07-01 13:44:08 -04:00

85 lines
1.9 KiB
Text

#>
newsyntax
@in:CH
@token:does
@CH:"function(Stack){
this.Next(Stack);
this.ChunkWrite(Stack,this.ParseExpression(Stack));
this.TestNext(Stack,\"Bracket\",\"TK_BOPEN\");
this.Move(Stack,2);
this.CodeBlock(Stack);
this.JumpBack(Stack);
this.OpenChunk(Stack);
while(this.CheckNext(Stack,\"Identifier\",\"otherwise\")||this.CheckNext(Stack,\"Identifier\",\"or\")){
if(this.CheckNext(Stack,\"Identifier\",\"otherwise\")){
this.OpenChunk(Stack);
this.ChunkWrite(Stack,\"else\");
this.Next(Stack);
this.TestNext(Stack,\"Bracket\",\"TK_BOPEN\");
this.Move(Stack,2);
this.CodeBlock(Stack);
this.JumpBack(Stack);
this.CloseChunk(Stack);
break;
}else{
this.OpenChunk(Stack);
this.ChunkWrite(Stack,\"elif\");
this.Move(Stack,2);
this.ChunkWrite(Stack,this.ParseExpression(Stack));
this.TestNext(Stack,\"Bracket\",\"TK_BOPEN\");
this.Move(Stack,2);
this.CodeBlock(Stack);
this.JumpBack(Stack);
this.CloseChunk(Stack);
}
}
this.CloseChunk(Stack);
}"
@Interpret:"function(AST,Token){
let Condition = this.Parse(AST,Token[3]);
if(Condition){
this.CondState(AST,Token[4]);
} else {
let Conds = Token[5];
if(Conds.length<1){return}
let DidCond = false;
let ElseCond = undefined;
for(let v of Conds){
if(DidCond){break}
if(v[0]==\"else\"){
ElseCond=v;
break;
}else if(v[0]==\"elif\"){
let Exp = this.Parse(AST,v[1]);
if(Exp){
DidCond=true;
this.CondState(AST,v[2]);
break;
}
}
}
if(!DidCond&&ElseCond){
this.CondState(AST,ElseCond[1]);
}
}
}"
<#
func equalsOneOrTwo(a){
does a == 1 {
log(`Yep! {a} == 1`);
} or a == 2 {
log(`Yes, {a} == 2`);
} otherwise {
does a <= 0 {
log(`Sorry, your number {a} must be greater than 0!`);
} otherwise {
log(`Nope. {a} != 1 or 2`);
}
}
}
equalsOneOrTwo(1);
equalsOneOrTwo(2);
equalsOneOrTwo(3);
equalsOneOrTwo(0);