7
\$\begingroup\$

(FB, or Functional-Basic, as found here Interpret Functional-Basic)

Task

Implement a linting program for FB, similar to JSLint for JavaScript.

Error types

Invalid identifier

Identifiers can only have upper and lower case ASCII letters.

Locations:

  • The identifier right after a let expression
  • The identifier in a for loop

Examples:

for (? in b) <-- ? is invalid
let b_b8 = ... <-- b_b8 is invalid

Undefined identifier

If you encounter an identifier that has not been defined in either a let expression or a for loop. Note that the identifier in a for loop only count in the expression following it.

Location:

  • Anywhere an identifier is used

Example program:

let b = a <-- nope
for (x in a) x+1 <-- nope, still don't know what `a` is
print x <-- gotcha! `x` is out of scope here

Unexpected EOL

End of line is the deliminator for statements in FB, so you can't have a statement span multiple lines (even if it's surrounded by parenthesis ()). Use this if you expected more but encountered a EOL char.

Locations:

  • Any line

Examples:

let b <-- missing the `= [statement]`
for ( <-- missing `x in y) [statement]`
print <-- print what?
print print sum <-- sum what?

Unexpected token

This is best explained by examples.

Locations:

  • Any line

Examples:

print * 2 <-- What is `*2` doing for me? I need a statement!
3**5      <-- `3*` expects a number, not a `*` symbol
for x in y <-- a little diffrent, but for
                     expects a pren `(`, not x

Improper type

Used when a list is expected and a number was encountered, or when a number is needed and you get a list. Lists in FB are only allowed to have numbers or other lists as children, so errors can occur here as well. Note that if you find an identifier that is valid, you should perform this check based on the id's type.

Locations:

  • Function application
  • for loops
  • The +-*/^ operators
  • List literal creation

Examples:

let y = [5]
let z = y + [[], [6]] <-- invalid, y is a list of numbers, right
                            hand side is a list of lists
print y + 1           <-- y is a list, not a number
sum 1                 <-- sum takes a list, not a number
[5] - [5]             <-- invalid, subtraction doesn't apply to lists
1 + []                <-- addition doesn't work with a list and a number
for (digit in 12) ... <-- `for` takes a list
let list = [00, 00]
let nlst = [list, 1]  <-- invalid
[1, []]               <-- invalid

Multiple assignment

This is when a for loop or let statements identifiers overlap with a currently defined identifier.

Locations:

  • In a for loop
  • In a let statement

Examples:

let y = 8
let y = 5 <-- invalid, y is already defined
for (y in []) <-- invalid, y is already defined
for (x in []) ...
let x = 8 <-- valid since the `x` in the for loop is out of scope

Nested for loop

Again, pretty self explanatory. FB doesn't allow nested for loops.

Locations:

  • In for loops

Examples:

for (x in [[1, 5], [2, 8]]) for (y in x) x*y <-- just invalid

Input format

If you are writing a function, you will receive one multi-line string. If you are writing a full program, you must take a multi-line input.

Output format

Output format is simply the error title, followed by the line number the error occurs on. You must show all errors in the program.

Format:

[error title] on line [line number, 1-indexed]

Example

This is one large example that should show all possible error types and outputs. Please let me know if I missed something.

Input

let bb8 = 0
let bbEight = lst

for (x in 
let x = 5*/5
let y = 4
let z = [5, 6] + y
let y = [[4]] + [y]
for (x in [[y, y+1], 5]) for (y in x) 

Output

Invalid identifier on line 1
Undefined identifier on line 2
Unexpected EOL on line 4
Unexpected token on line 5
Improper type on line 6
Improper type on line 7
Multiple assignment on line 7
Nested for loop on line 8
Multiple assignment on line 8
Multiple assignment on line 8
Improper type on line 8
Unexpected EOL on line 8

Note that thees checks may come in any order, and if the same error occurs multiple times on the same line, you must print the error multiple times as well.

I hope this isn't to difficult

Edits

I have removed the Empty line error since it didn't make sense, and nobody is working on the problem (to my knowledge).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.