Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELSE operator #6

Closed
vooon opened this issue Mar 14, 2014 · 6 comments
Closed

ELSE operator #6

vooon opened this issue Mar 14, 2014 · 6 comments
Labels
enhancement Something new (not a bug fix) is being requested

Comments

@vooon
Copy link

vooon commented Mar 14, 2014

The else statement extremely necessary.

Example syntax:

IF stmt {
 run foo.
}
ELSE {
 run bar.
}

IF stmt {
 run foo.
}
ELSE IF stmt2 {
 run baz.
}
@Dunbaratu
Copy link
Member

I'd second this. I've had to write weird code at times because of the lack of an else.

@erendrake
Copy link
Member

We have two operators here ELSE and ELSE IF. Which is more important to you so we can prioritize?

@vooon
Copy link
Author

vooon commented Mar 22, 2014

I think only ELSE. With proper implementation ELSE IF is obtained automatically.

In terms of kRISC.tpg:

ELSE -> @"else";
if_else_stmt -> IF expr instruction_block ELSE instruction_block EOI?;

@marianoapp
Copy link
Contributor

That's right, but the ELSE part in the grammar should be optional, like this:

if_stmt -> IF expr instruction_block (ELSE instruction_block)? EOI?;

@Dunbaratu
Copy link
Member

I've implemented the ELSE in my own local copy, but I don't understand how to push two different separate pull requests to github that are both pending at the same time. It keeps trying to merge my changes for this issue with my previous pull request for a different issue. Do I need to make a branch to keep that from happening? All my past experience has been with SVN and CVS so I don't quite 'get' Github yet.

At any rate, if I can figure out how to get my code into a second pull request, I have this solved so that the following test script works correctly:

//KOS
//  A script with the purpose of testing that 'if' and 'else' work right:

set a to 10.
set b to 20.
set c to 40.

print "IF,  one line body, true check:".
print "is " + a + " = 10?".
if a = 10
  print "  yes.".
print "IF,  one line body, false check:".
print "is " + b + " = 10?".
if b = 10
  print "  yes.".

print "IF,  two line body requiring braces:".
print "is " + a + " = 10?".
if a = 10 {
  print "  yes.".
  set doNothing to 1. // just to make a reason for the braces.
}.

print "if/else, one-liners, true check:".
print "is " + a + " = 10?".
if a = 10
  print "  yes".
else
  print "  no".

print "if/else, one-liners, false check:".
print "is " + b + " = 10?".
if b = 10
  print "  yes".
else
  print "  no".

print "if/else, multi-line bodies, true check:".
print "is " + a + " = 10?".
if a = 10 {
  print "  yes".
  set doNothing to 1. // just to make the body longer than 1 stmt.
} else {
  print "  no".
  set doNothing to 1. // just to make the body longer than 1 stmt.
}.

print "if/else, multi-line bodies, false check:".
print "is " + b + " = 10?".
if b = 10 {
  print "  yes".
  set doNothing to 1. // just to make the body longer than 1 stmt.
} else {
  print "  no".
  set doNothing to 1. // just to make the body longer than 1 stmt.
}.

print "nested if/else, one-liners, hits first term:".
print "what is " + a + " ?".
if a < 15
  print "  less than 15".
else if a < 20
  print "  in the range [15,20)".
else if a < 30
  print "  in the range [20,30}".
else 
  print "  >= 30".

print "nested if/else, one-liners, hits third term:".
print "what is " + b + " ?".
if b < 15
  print "  less than 15".
else if b < 20
  print "  in the range [15,20)".
else if b < 30
  print "  in the range [20,30}".
else 
  print "  >= 30".

print "nested if/else, one-liners, hits final else:".
print "what is " + c + " ?".
if c < 15
  print "  less than 15".
else if c < 20
  print "  in the range [15,20)".
else if c < 30
  print "  in the range [20,30}".
else 
  print "  >= 30".

print "nested if/else, mix of one-line and braces, hits middle term:".
print "what is " + b + " ?".
if b < 10
  print "  less than 10.".
else if b < 30 {
  print "  in the range [10,30}".
  set doNothing to 1. // pointless line to force need for braces.
} else {
  print "  >= 30".
  set doNothing to 1. // pointless line to force need for braces.
}.

I have to say, @marianoapp, the kRISC code is nice and easy to understand (provided you know a few basic things about what assembly language programs typically look like) and I'm impressed with the internals now that I've dived into them a bit to make this work. The ELSE implementation worked right on the first attempt without headaches, just from basic guesswork on my part about how to implement it, looking at how the else-less if worked already.

@marianoapp
Copy link
Contributor

Thanks, I appreciate it.
Closing this as it was implemented along the fix for #20.

erendrake pushed a commit that referenced this issue Apr 9, 2015
Fixed a bug when a script calls another with args.
Dunbaratu referenced this issue in Dunbaratu/KOS-1 Apr 18, 2015
Pulling changes from the main repo
erendrake pushed a commit that referenced this issue May 13, 2015
Fixed it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something new (not a bug fix) is being requested
Projects
None yet
Development

No branches or pull requests

4 participants