Commit Graph

1048 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 88134e8cab Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-17 17:02:17 +02:00
Tristan B. Velloza Kildaire 9b55b3dec4 Containers
- `Struct`'s `MCloenable` API adhered to
2023-07-17 17:01:10 +02:00
Tristan B. Velloza Kildaire b09aa3add0 Data
- `Variable` and `VariableAssignment` now uses the new `MCloneable` API
2023-07-17 17:00:34 +02:00
Tristan B. Velloza Kildaire a4bd462d14 Expressions
- Added notes about where parenting must manually be done
2023-07-17 16:57:42 +02:00
Tristan B. Velloza Kildaire 7acb8e6308 Expressions
- `BinaryOperatorExpression`, `CastedExpression` and `IntegerLiteral`  now implements the new `MCloneable` API
2023-07-17 16:56:10 +02:00
Tristan B. Velloza Kildaire 254c5ee6b9 Mcro
- Made `MCloneable`'s `clone()` require a `Container` argument of which the final cloned `Statement` must parent-itself to
2023-07-17 16:51:22 +02:00
Tristan B. Velloza Kildaire 921235ab22 DGen
- If `typeTransform(Type)` is called on an unknown type then print out the type in the error message asx well
2023-07-17 16:35:32 +02:00
Tristan B. Velloza Kildaire a42ec98625 DGen
- If `typeTransform(Type)` fails then the debug text should be in the error coloring
2023-07-17 16:31:49 +02:00
Tristan B. Velloza Kildaire fc4f22ebda Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-17 16:19:51 +02:00
Tristan B. Velloza Kildaire 8a11eabd96
🧠 Feature/Meta: Cloneable (round 1) (#21)
* Data

- Moved AST manipulation imports to the top
- Made `VariableAssignment` cloneable
- Made `Variable` cloneable

* Data

- When trying to `clone()` an `Expression`, do a runtime type check to check if we can (else `null` is used)

* Expressions

- Moved AST-manipulation related import to the top

* Expressions

- `BinaryOperatorExpression` now overrides `clone()` from `MCloneable`

* Expressions

- `IntegerLiteral` now implements `MCloneable`

* Expressions

- `CastedExpression` now implements `clone()` for `MCloneable`

* Containers

- Moved all AST maniuplation-related imports to the top

* Containers

- Made `Struct` support `MCloneable`'s `clone()` method

* Data

- Only clone the `VariableAssignment` if the `Variable` has one

* Containers (unit test)

- Added test which tests the `clone()` on a `Struct`
2023-07-17 16:16:18 +02:00
Tristan B. Velloza Kildaire f1aaaf1088 Expressions
- Made the `Expression` class abstract
- Removed irrelevant TODOs and method
- Removed uneeded constructor

VariableExpression

- Removed unused junk
2023-07-17 15:01:46 +02:00
Tristan B. Velloza Kildaire 2411969955 Data
- Removed unused type `ArgumentList`
2023-07-17 14:41:18 +02:00
Tristan B. Velloza Kildaire f4f412ce1a TypeChecker
- Removed old type checking code for variable declarations with assignments
- Removed old type checking code for standalone variable assignments
2023-07-17 13:11:31 +02:00
Tristan B. Velloza Kildaire b0135cd341 TypeChecker
- Removed some dead code
- Removed now-completed FIXME/TODO
2023-07-17 13:07:36 +02:00
Tristan B. Velloza Kildaire 57a44bfd4f TypeChecker
- Added comment describing the process used
- Removed now-completed TODO
2023-07-17 13:03:00 +02:00
Tristan B. Velloza Kildaire a73e8308db Test cases
- Removed now-unneeded explicit casts on literal values in `simple_function_recursion_factorial.t`
2023-07-17 12:40:52 +02:00
Tristan B. Velloza Kildaire 155298e9ee TypeChecker
- If we have a `LiteralValue` and a non-`LiteralValue` then coerce the `LiteralValue` towards the non`-LiteralValue` via `typeEnforce()`
- This should allow the correct range checking of literal values within the range of the to-type and not require annoying explicit casts
2023-07-17 12:37:32 +02:00
Tristan B. Velloza Kildaire 7f902b9fcf Test cases
- Removed explicit cast from `simple_function_recursion_factorial.t`
2023-07-17 11:42:18 +02:00
Tristan B. Velloza Kildaire b8883ddb27 Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-17 11:23:41 +02:00
Tristan B. Velloza Kildaire 4cae2bb6ce Parser
- Removed now-completed TODO
2023-07-17 10:55:32 +02:00
Tristan B. Velloza Kildaire 9f94f4ff51 Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 20:35:20 +02:00
Tristan B. Velloza Kildaire 0b3a06efde
🐞 Bugfix: expect(string) should throw ParserException atleast (#20)
* Parser

- Removed commented-out code

* Parser

- Removed `isUnitTest` variable and how `expect(string)` relies on it

* Parser

- Now throw a new `ParserException` instead of a `TError` when calling `expect(string)`

* Parser

- Made `expect(string)` not static

* TypeChecker

- Implemented `expect(string)` which throws a `TypeCheckerException` in a similar fashion to `Parser`'s `expect(string)`

* Dependency

- Implemented `expect(string)` which throws a `DependencyException` in a similar fashion to `Parser`'s `expect(string)`

Exceptions (dependency)

- Added enum member `GENERAL_ERROR` to `DependencyError`

* Parser (unit tests)

- Made more specific
2023-07-16 20:33:51 +02:00
Tristan B. Velloza Kildaire 1b72cbfb9a Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 19:59:35 +02:00
Tristan B. Velloza Kildaire fa9399b90d Test cases
- Fixed `simple_function_decls.t` to include the required (but missing) `return` statement
2023-07-16 19:58:40 +02:00
Tristan B. Velloza Kildaire abe26a60fe Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 19:55:12 +02:00
Tristan B. Velloza Kildaire b18efdc76c Tets cases
- Added missing `return` to function `banana` in `simple_functions.t` test case
2023-07-16 19:54:47 +02:00
Tristan B. Velloza Kildaire 3b5ee76ab3 Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 19:50:03 +02:00
Tristan B. Velloza Kildaire 352eada94a Parser (unit tests)
- Because this causes `expect(string)` to be called (this unit test) we must catch `TError` as that is what `expect(string)` throws
- We should update this (see #147)
2023-07-16 19:49:13 +02:00
Tristan B. Velloza Kildaire d9486b11c1 Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 19:44:09 +02:00
Tristan B. Velloza Kildaire a1443c4c48 Parser (unit tests)
- Updated unit test which was meant to check for a greater number of body statements of the `Function` at question
2023-07-16 19:43:44 +02:00
Tristan B. Velloza Kildaire 2107220d1d Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 19:22:45 +02:00
Tristan B. Velloza Kildaire f8bfd27ece
🐞 Bugfix: Recursively check for return statemen tprescence (#19)
* Parsing

- Added a TODO for where the fix needs to be

* Parser

- Implemented `findOfType(TypeInfo_Class statementType, Container from)` which given a type of `Statement` to look for and a `Container` of which to search with in. This method will recursively search down the given container and look for any statements which are a kind-of (`isBaseOf`) the requested type. it will return an array of `Statement` (`Statement[]`) of the matches.
- Implemented `existsWithin(TypeInfo_Class statementType, Container from)` given a type of `Statement` to look for and a `Container` of which to search with in. This method will recursively search down the given container and look for any statements which are a kind-of (`isBaseOf`) the requested type. It will return `true` if any matches are found.
- We now will recursively explore the `Function`'s body statements in search of a `ReturnStmt`
2023-07-16 19:21:09 +02:00
Tristan B. Velloza Kildaire a144e544f9 Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-16 18:28:28 +02:00
Tristan B. Velloza Kildaire ddedc0d806
🐞 Bugfix: Ensure that typed functions contain a return statement (#17)
* Parser

- Added TODO for checking for non-`void` function's return statements

* Parser

- If a `ReturnStmt` is not found after calling `parseFuncDef()`

* Parser

- Added a FIXME comment that needs to be completed soon

* Parser

- Only check that a `ReturnStmt` is present in a function definition when `wantsBody` is true ELSE we'd expect it for `efunc` `extern`'d statements
- Fixed a unit test which had a missing `return <expr>`
- Added a unit test which tests for a function definition with a body with a non-void return type that it DOES fail
- Removed two now-completed TODOs and FIXMEs

Test cases

- Fixed test case in `typecheck/simple_function_call.t` to now include the required `return <expr>` statements
2023-07-16 18:22:56 +02:00
Tristan B. Velloza Kildaire b01ff34daa TypeChecker
- Removed assertion check, rather let the exception thrown handle the error
- Only after we know the finally-parenting `Container` is a `Function` (should we reference `funcContainer`
2023-07-13 13:31:46 +02:00
Tristan B. Velloza Kildaire dd66d2e47e Test cases
- Added two new checks for checking the return type of a function and matching a `ReturnStmt`'s expression's type to it
2023-07-12 11:02:56 +02:00
Tristan B. Velloza Kildaire 0a4cec2013 TypeChecker
- `ReturnStmt` now uses `typeEnforce()`
2023-07-12 10:59:40 +02:00
Tristan B. Velloza Kildaire adae68e950 TypeChecker
- Added FIXME where the `typeEnforce()` call need to be made for the `ReturnStmt`'s return expression's type to match or be checked-against the containing `Function`'s
2023-07-12 10:07:03 +02:00
Tristan B. Velloza Kildaire 11c119d62a Merge branch 'vardec_varass_dependency' into feature/universal_coercion 2023-07-12 10:05:30 +02:00
Tristan B. Velloza Kildaire 4c99b56677 TypeChecker
- Fixed the `Function` `Container` resolution for the `ReturnStmt` to now find the nearest `parent` in its parenthood tree which is a `Function`, extract the `Function`'s name and then use that where needed
2023-07-12 09:55:59 +02:00
Tristan B. Velloza Kildaire 3e101ac5f8 Resolution
- Implemented `findContainerOfType(TypeInfo_Class, Statement)` which, given a type-of `Container` and a starting `Statement` (AST node) this will swim upwards to try and find the first matching parent of which is of the given type (exactly, not kind-of).
2023-07-12 09:55:14 +02:00
Tristan B. Velloza Kildaire c0920f2fdb
🐞️ Functions: Expressionless return and enforcing requirement (#7)
* Parser

- Added a TODO in `parseReturn()` for issue #113

* Data

- The `ReturnStmt` now has a default constructor which is for cases where one doesn't want to provide an expression (for expressionless returns)

Parser

- `parseReturn()` now supports expressionless returns

Test cases

- Added `simple_return_expressionless.t` to test expressionless return statement

* Data

- Added a method `hasReturnExpression()` to `ReturnStmt` which returns `true` if the return statement has an expression attached, `false` otherwise

* Dependency

- When processing a `ReturnStmt` only run do dependency generation for the return statement's expression IF it has one

* Instruction

- Made `ReturnInstruction` have a constructor which takes in no `Value` instruction (intended for return expression)
- Added a `hasReturnExpInstr()` to `ReturnInstruction`such that during typechecking/codegen we can check for it

* TypeChecker

- Added a TODO regarding the missing typechecking for `ReturnStmt` typechecking. Added notes on how we'd go about this.
- Fixed crash due to assuming there was always an expression on the stack that could be popped off for generating a `ReturnInstruction` (this is not the case when the return statement is expressionless)

* Tests

- Added a typecheck test for `simple_return_expressionless.t`

* TypeChecker

- Update `isSameType(Type t1, Type t2)` to check if the actual types of both `Type` objects are the same as a last resort
- Added a `NOTE` comment on how `isSameType(Type t1, Type t2)` is implemented

- Added typechecking code for `ReturnStmt` and updated the code generation with it. We now do the following:
    1. We extract the container of the `ReturnStmt` and cast it to a `Function`; if it is not a `Function` we throw an error because you cannot have a `ReturnStmt` appear in a non-`Function` container
    2. We extract the function's name relative to it container (the function's container) for use of it in error messages
    3. Next, we get the return type of the function and do the following:
        a. If the return type is `void`
            i. If the return has an expression we throw an error
            ii. If the return has NO expression we pass typechecking and generate the `ReturnInstr`
        b. If the return type is non-`void`
            i. If the return has an expression we ensure that its type matches that of the function's return type and generate the `ReturnInstr`
            ii. If the return has NO expression we raise an exception as one is expected
    4. If we pass and got here then we set the `ReturnInstr`'s context and `addInstrB(returnInstr)`

* Test cases

- Added test case `simple_return_type.t` which is here to test our return type checking

* - Updated `.gitignore`

* Parser

- Use `lexer` for all `Token`-based operations
2023-07-11 21:43:21 +02:00
Tristan B. Velloza Kildaire 400bce8035 TypeChecker
- Added a TODO
2023-07-11 21:07:44 +02:00
Tristan B. Velloza Kildaire d1c69f0d0d TypeCheck
- Completed TODO comment
2023-07-11 19:02:42 +02:00
Tristan B. Velloza Kildaire 434011f617 TypeChecker
- Type checking code for `FunctionCall`
2023-07-11 18:40:33 +02:00
Tristan B. Velloza Kildaire a65033e9dd TypeChecker
- Print out a debug message when attempting to coerce a `StackArray` to a `Pointer`
- Fixed the error message thrown when a `StackArray` could not be coerced to a `Pointer` due to the component type != ptr's referred type
- `FunctionCall` handling now has the `canCoerceStackArray()` code disabled and uses the `typeEnforce()` method
2023-07-11 18:28:22 +02:00
Tristan B. Velloza Kildaire 9f28ce023f TypeChecker
- `attemptCoercion(Type, Value)` will now ensure firstly that the `StackArray`'s component type matches that of the `Pointer`'s referred type, if not throw an exception, if so, then return a `CastedValueInstruction`
2023-07-11 11:58:52 +02:00
Tristan B. Velloza Kildaire b2419ca51f TypeChecker
- Implemented ` isStackArrayType(Type typeIn)`
- WIP: Added a check for handling `StackArray -> Pointer` coercion to `attemptCoercion(Type, Value)`
2023-07-11 11:41:52 +02:00
Tristan B. Velloza Kildaire 0094dd58f2 TypeChecker
- Marked related issue
2023-07-11 11:11:30 +02:00
Tristan B. Velloza Kildaire 6730da28f4 TypeChecker
- Fixed explanation
2023-07-11 11:10:34 +02:00