Commit Graph
151 Commits
Author SHA1 Message Date
Geoff Romer 883f2a3ecd Update IfExpression to follow AST conventions (#1167) 2022-04-12 12:28:48 -07:00
Geoff Romer 8f9638d759 Initial support for statically-sized arrays (#1158) 2022-03-31 10:55:34 -07:00
210856dd57 Generic classes (#1124)
* start of generic classes

* fix regressions

* class functions in interfaces

* access to class function on interface from class parameter

* more stuff working for generic classes

* fixing bugs

* update TypeEqual for generic classes

* fixing bugs and finding new ones

* disable unqualified access to members from other members for now

* minor edits

* bug fixes

* introduce compile_time_value to use in type checker instead of constant_value

* cleanup

* put a CHECK back in

* failure test cases for the new FATAL_COMPILATION_ERROR

* change a runtime FATAL into a FATAL_COMPILATION_ERROR

* Update executable_semantics/ast/declaration.h

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/action_stack.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/resolve_names.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* responses to reviews

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* rename compile_time_value to symbolic_identity

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* more edits from review

* Apply suggestions from code review

Co-authored-by: Geoff Romer <gromer@google.com>

* review responses

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* const impl_scope for TypeCheckChoiceDeclaration

* add some const

Co-authored-by: Jon Meow <jperkins@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-03-29 13:09:41 -04:00
aa8a5f174d Replaced std::exit() with return Carbon::ErrorOr for expected errors like invalid syntax (#1120)
* Replaced std::exit() with return llvm::Expected/llvm::Error<T> for expected errors like invalid syntax.

* Use llvm::formatv() for formatting lexer error messages.
x

* Addresed merge errors.

* Fixed impl scope.

* Made ErrorBuilder::operator<< nodiscard, to catch code forgetting 'return' in 'return FATAL_COMPILATION_ERROR()'.

* FatalComplationError() -> ParseAndLexContext::RecordLexerError().
Other usages of ERROR_TOKEN in lexer.lpp were actually supposed to be END_OF_FILE.

* Update executable_semantics/syntax/parse_and_lex_context.h

Co-authored-by: Jon Meow <jperkins@google.com>

* Code review fixes.

* Update executable_semantics/syntax/parser.ypp

Co-authored-by: Jon Meow <jperkins@google.com>

* More code review fixes.

* Update executable_semantics/interpreter/type_checker.h

Co-authored-by: Jon Meow <jperkins@google.com>

* Yet more code review fixes...

* Update executable_semantics/syntax/lexer.lpp

Co-authored-by: Jon Meow <jperkins@google.com>

* code review comments

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* Apply suggestions from code review

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/syntax/lexer.lpp

Co-authored-by: Jon Meow <jperkins@google.com>

* code review

* code review

* Apply suggestions from code review

Co-authored-by: Jon Meow <jperkins@google.com>

* formatted code

* review comments

* Switched to the new ErrorOr<V> error implementation

* code review comments

* fixed comment

* restored ostream.h as #976 makes the change unnecesary

* review comments

Co-authored-by: Jon Meow <jperkins@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-03-22 16:17:04 -04:00
Jon Meow e5a87af6fe clang-tidy without config changes (#1147)
Separating out existing fixes because of #1148 which turns on more.
2022-03-22 11:13:27 -07:00
20b272446f Let variables (#1095)
* Modify parser and AST nodes to include let statement

* Implement let variables

* Remove redundant code in fail_match_choice test

* Add comment for has_value_category()

* Apply suggestions from code review

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>

* clang-format changes

* Update comments for new BindingPattern methods

* Implement nested vars in patterns

* Apply suggestions from @geoffromer's code review

Co-authored-by: Geoff Romer <gromer@google.com>

* Implement changes from code review.

* Remove maybe_var_pattern grammar rule

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-03-11 16:30:24 -05:00
Geoff Romer 93842ad878 Eliminate run-time errors from PatternMatch (#1126) 2022-03-10 16:46:43 -08:00
Richard Smith 2325c50b3b Support for if ... then ... else following #911. (#944) 2022-03-03 16:34:34 -08:00
7cce1bd124 interfaces, impls, and constrained generics (basics) (#1073)
* interfaces, impls, and constrained generics (basics)

* separate type checking into declare vs. type check, removing redundancy

* external impls

* added impl scopes to handle generics calling generics

* cleanup

* more cleanup

* Update executable_semantics/testdata/interface/external_impl_point_vector.carbon

Co-authored-by: josh11b <josh11b@users.noreply.github.com>

* Update executable_semantics/testdata/interface/generic_call_generic.carbon

Co-authored-by: josh11b <josh11b@users.noreply.github.com>

* Update executable_semantics/testdata/interface/tuple_vector_add_scale.carbon

Co-authored-by: josh11b <josh11b@users.noreply.github.com>

* Update executable_semantics/testdata/interface/vector_point_add_scale.carbon

Co-authored-by: josh11b <josh11b@users.noreply.github.com>

* change ImplementationDeclaration to ImplDeclaration

* remove impl_type_value

* split NamedEntity into two

* changed GetName to be a free function

* adding comments

* more edits to respond to review

* introduce ImplBinding, remove punning on GenericBinding

* new test case and some minor edits

* refactor GetMember and GetField to move impl logic to interpreter

* remove commennt

* change EntityView to ImplBinding in FieldAccess...

* move ImplBinding

* review response

* added example to impl_scope.h

* minor edits

* Update executable_semantics/interpreter/field_path.h

Co-authored-by: Geoff Romer <gromer@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* Update executable_semantics/ast/expression.h

Co-authored-by: Geoff Romer <gromer@google.com>

* Update executable_semantics/ast/expression.h

Co-authored-by: Geoff Romer <gromer@google.com>

* Update executable_semantics/ast/generic_binding.h

Co-authored-by: Geoff Romer <gromer@google.com>

* more edits from review

* review response

* Update executable_semantics/ast/static_scope.h

Co-authored-by: Geoff Romer <gromer@google.com>

* remove ImplType, renamed node_view to value_node

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-03-02 15:58:45 -05:00
Jeremy G. Siek ac0b810bf3 Adds basic support for class functions and methods. (#1057)
* adding methods to the ast

* pre commit stuff?

* implementation of class functions

* implemented methods

* some cleanup

* more cleanup

* add newlines in test programs

* pre-commit fixups

* added include of return_term.h

* a test of a method calling another method

* replacing Member with Declaration

* removing the member.h etc files

* clarify a type annotation

* update uses of FunctionDeclaration

* remove ReturnTarget, no longer needed

* more cleanup

* more cleanup

* yet more cleanup, playing with pre-commit

* did a pre-commit run --all-files

* fixed const issue

* remove comment

* checking dependencies in BUILD files and headers

* pre-commit working now

* refactor NominalClassType to just hold a pointer to the class declaration

* remove Member from rtti

* responding to Geoffreys review

* change field_types to a non-member function
2022-02-05 12:30:13 -05:00
Darshal Shetty 4479c55305 Pointers (#1060)
* Naive pointer implementation

* Bug fixes and better pointer tests

* Remove debug print statement

* Implement changes suggested by @geoffromer

Also add a failing test case that tests applying the address-of operator
to an rvalue.
2022-02-05 10:27:54 -05:00
Geoff RomerandJon Meow 1723b4e0b2 Hide Interpreter in .cpp (#1036)
This improves encapsulation, and makes Interpreter lifetimes clearer (and shorter).

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2022-01-26 09:57:55 -08:00
Geoff RomerandJon Meow c311c8849c Use static name resolution in Interpreter (#1022)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2022-01-21 09:48:11 -08:00
Geoff Romer 461e178273 Store named constant values in the AST (#1011)
This enables us to stop using `Env` in the typechecker. As a byproduct, this commit also restructures the interpreter to handle run-time global initialization as part of ordinary execution, using the Action stack.
2022-01-13 11:07:28 -08:00
Geoff Romer 50263483d8 Add name accessor to NamedEntityView (#994)
This required changing BindingPattern::name() to return `"_"` instead of nullopt when representing a `"_"` binding.

Semi-related drive-by fixes:
- Update BindingPlaceholderValue to expose a NamedEntity instead of a string name, and stop exposing its type.
- Drop the unused SourceLocation parameter of ValueEqual
2022-01-04 15:53:11 -08:00
Geoff Romer fd45b089e9 Add typechecking of type declarations (#984)
Also explicitly model the types of expressions whose values are class or choice types, and make those be the static_type of the type declaration. This ensures that the static_type of a NamedEntity consistently corresponds to the static type of an IdentifierExpression using that name.
2021-12-13 16:19:52 -08:00
Geoff Romer e65e85b15d Use AST nodes as generic arg deduction keys (#982) 2021-12-10 12:19:06 -08:00
Geoff RomerandJon Meow 17e0a1afb9 Implement static name resolution (#958)
This doesn't actually use the results of name resolution, but it does verify that they are present.
Also ensures that name resolution and type checking are applied to deduced function parameters and the implicit call to `Main()`.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-11-30 13:33:54 -08:00
Geoff RomerandJon Meow dc5e62fc7a Support parsing and testing unimplemented expressions (#957)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-11-30 12:29:15 -08:00
Geoff Romer be0c1e9da6 Move value-category checking to compile time (#960) 2021-11-29 17:07:50 -08:00
Geoff Romer 3e188c8562 Express prelude using Carbon syntax (#955)
This should be clearer, more maintainable, and more scalable than building the prelude AST by hand.
2021-11-29 13:56:55 -08:00
Geoff Romer 3f7e1cd3fb Clarify and simplify handling of lvalues (#956)
- Rename `PointerValue` to `LValue` to reflect how it's actually used. We can introduce a `PointerValue` type when we add support for actual pointer values.
- Remove support for pattern assignment. It's unclear if Carbon will support this, and even if we do, it raises questions that should first be addressed in a language proposal, like "is the left-hand side of `(x, y) = (1, 2)` an lvalue, or a pattern, or both, or something else entirely?"
2021-11-23 14:13:08 -08:00
Geoff Romer 9642d7ad05 Factor out ActionStack from Interpreter (#951) 2021-11-17 12:59:16 -08:00
Geoff Romer 7a5b8434c8 Define a base class for all AST nodes. (#947)
Also implement code-generation to manage the resulting boilerplate.
2021-11-16 11:54:47 -08:00
Geoff Romer d854fb93cb Factor out AST node for function return types. (#912)
This enables us to stop treating the return type as a Pattern (which is really isn't), treat return types more consistently with other static types in the typechecker, and drop ReturnTypeContext.

Additional changes:
- Merge TypeCheckFunDef with TypeOfFunDef.
- Handle implicit conversions in `return` statements.
- Require function type literals to have an explicit `->`.
- Move consistency check for omitted returns from TypeChecker to ResolveControlFlow.
2021-11-12 11:30:31 -08:00
Geoff RomerandJon Meow a4aff26821 Stop allocating Actions on the Arena (#934)
This enables us to manage local variables in Carbon using C++ RAII.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-11-10 16:35:06 -08:00
Geoff Romer a9bb06ca14 Make NamedEntityInterface non-movable (#946) 2021-11-10 09:33:00 -08:00
Jon Meow e8d9b7675b Merge ClassDefinition and ClassDeclaration (#940) 2021-11-09 15:24:22 -08:00
Jon Meow 73eea8e47a Merge UnwindTo and UnwindPast logic (#926)
Also shifts the DeallocateScope concept into UnwindTodoTop since that's how it's used.
2021-11-08 09:22:38 -08:00
Jon MeowandGeoff Romer 27e084d37a Start populating named entities in relevant locations. (#919)
This starts detecting naming collisions as a consequence of being able to determine when the name is declared twice in a given scope.



Co-authored-by: Geoff Romer <gromer@google.com>
2021-11-03 08:04:13 -07:00
Jon Meow 904774fbb8 Collapse Sequence into Block. (#927) 2021-11-01 14:59:39 -07:00
Jon Meow bbd4940e6d Be more explicit about AST types (#921)
This was born out of wanting FunctionDeclaration to explicitly have a Block for a body, and became a bit more of specifying types around. Note this forces exec_program to generate a Block for print()'s body, which is probably more correct as now we can expect a standard FunctionDeclaration AST structure, even for the built-ins.

There is a syntactic change here: a continuation's body is now a Block, not just a Statement. I've added an example disallowed test case. I think this is more reasonable syntax.

Other than that, note that optional_else now generates a valid Block. This has me thinking about whether we can eliminate Sequence, but that seemed well out of scope for this.
2021-10-28 13:54:45 -07:00
Geoff Romer 70a7839a31 Fix bug from #909 (#924) 2021-10-27 17:09:03 -07:00
Geoff Romer 0df9e8666c Factor AllocationId out of Address (#916)
This lets us statically distinguish between code that works with arbitrary `Address`es and code that can only work with pointers to separately-allocated storage, and so we no longer need to worry about the latter code crashing at run-time (as `Heap::Deallocate` did) or silently doing the wrong thing (as `Heap::PrintAddress` did) if it's given the wrong kind of `Address`.
2021-10-27 16:40:17 -07:00
Geoff Romer 0da907fd2a Unify StructElement and VarValues (#909) 2021-10-27 16:33:38 -07:00
Geoff RomerandJon Meow 3bec7f8dc0 Unify Action and Scope stacks, and eliminate Frame (#880)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-10-21 14:55:41 -07:00
Geoff Romer 3677b76cb8 Perform implicit conversions at run time (#903)
This resolves the "cheating" in #870.
2021-10-21 12:47:49 -07:00
Jon MeowandGeoff Romer 057b9fe253 Clean up remaining executable_semantics clang-tidy warnings (#906)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-10-20 10:25:22 -07:00
Geoff RomerandJon Meow 79e3d284b4 Move pattern interpretation to compile time (#904)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-10-20 10:05:17 -07:00
Jon Meow ffa4e76ede Add underscores to private data members (#898)
This is the last PR I plan to have focused on #720
2021-10-19 14:27:10 -07:00
Jon Meow a9eed3dbf1 Pass the flag instead of using a global. (#893)
Arguably missed in #769 

Note, this is reminding me we have more class members to rename for `_`, but I felt it's best to use the new naming instead of adding more to clean up.
2021-10-19 09:16:57 -07:00
Geoff Romer a3eac75a5b Unify function declarations and definitions. (#896) 2021-10-18 16:21:39 -07:00
Geoff Romer c4d7adde9a Drop CopyVal (#895)
Values are immutable, so they should never need to be copied.
2021-10-18 15:58:35 -07:00
Jon Meow 5b67a08d97 Refactor Action accessors (#891) 2021-10-18 11:02:09 -07:00
Jon Meow eeed6301d6 Refactor Value accessors (#892) 2021-10-18 10:50:56 -07:00
Jon Meow 8bda2ca432 Refactor Pattern and Member accessors. (#889) 2021-10-18 10:25:52 -07:00
Jon Meow b278e4edcf Refactor Statement accessors (#890) 2021-10-18 10:25:35 -07:00
Geoff Romer bb28d37eed Drop support for named tuple fields (#886)
Rationale: Based on the status of #478 and #505, Carbon won't have this feature for a while, and it will be simpler not to support it on spec in the meantime.
2021-10-15 13:19:57 -07:00
Jon MeowandGeoff Romer 9007bfcb88 Run clang-tidy on the interpreter directory (#885)
`// NOLINT` is added on type aliases in stack.h and dictionary.h to allow lower_snake_case naming -- this didn't feel like a check worth disabling in spite of false positives.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-10-15 08:54:53 -07:00
Jon Meow 55ecb62ce5 Refactor Expression accessor/mutator style (#883) 2021-10-13 16:56:27 -07:00