Commit Graph
94 Commits
Author SHA1 Message Date
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
pk19604014 2ea7de8a17 Fixed a couple crashes surfaced by running a structured fuzzer on executable_semantics parse/type-check/execute logic. (#1106)
* Fixed a couple simple crashes surfaced by running a structured fuzzer on executable_semantics parse/type-check/execute logic.

* var a: b: auto; -> var a: auto;
2022-03-04 14:45:50 -05:00
Richard Smith 2325c50b3b Support for if ... then ... else following #911. (#944) 2022-03-03 16:34:34 -08:00
Richard Smith 8c60ad2e19 Implement the current set of rules for precedence partial ordering (#1096)
In the process, switch to an unambiguous grammar, using the
precedence-climbing method for operator precedence (suitably modified to
handle a partial precedence order) rather than Bison %precedence /
%prec.
2022-03-03 13:36:35 -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
Jon Meow 385ac7da86 Refactor lit testing to generate per-file tests. (#1063)
This switches to generating per-file targets such as `//executable_semantics/testdata:tuple/equality_false.carbon.test`, instead of one test covering all files. Essentially this relies on bazel instead of lit to handle test parallelization.
2022-02-08 11:01:27 -08: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
Jon Meow 9009ae5e1b Cleanup up lit_test and require explicit tool paths (#1049)
I think lit_test accrued a bit of cruft as I switched approaches... I still may go further, but this is particularly fixing a bug where `FileCheck` (instead of `%{FileCheck}`) should've failed in tests.
2022-01-27 16:27:30 -08:00
Jon Meow d76fe462f6 Have FileCheck dump all input on errors. (#1050)
I'm hoping this improves debugability.
2022-01-27 12:49:44 -08:00
Jon Meow 7dcb7a3be8 Drop line because it's a set so arbitrary sorting applies (#1043) 2022-01-26 15:13:26 -08:00
pk19604014andGeoff Romer 2017eb4da0 Initial implementation of block string literals following lexical_conventions/string_literals.md. (#1028)
* Initial implementation of block string literals following lexical_conventions/string_literals.md.

Enabled yyinput() in flex to implement parsing.
Added ParseBlockStringLiteral() helper to handler further transformations such as indenting.
Modified formar_grammar to support single-quoted strings to prevent a failure on lexer.lpp.

* Fixed _find_string_end quote parameter type int -> str.

* Update executable_semantics/syntax/BUILD

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

* Addressed code review comments - split table-drived test into individual tests, renamed constants to match style guide.

* Addressed code review comments -- using EXPECT_THAT_EXPECTED() in tests, lexer comments and code cleanup.

Co-authored-by: Geoff Romer <gromer@google.com>
2022-01-21 13:11:18 -05: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 c89ce1d570 Handle identifier value category properly. (#986) 2021-12-15 13:27:36 -08:00
Geoff Romer daf8729a75 Use static names in typechecker. (#985) 2021-12-14 11:26:53 -08:00
Geoff Romer 8e6c209a28 Use resolved names in typechecker (#974)
As a byproduct, replace NamedEntity with a type-erasing wrapper NamedEntityView, eliminate virtual inheritance from the AST, and eliminate interfaces from gen_rtti.
2021-12-13 16:35:30 -08:00
Jon Meow beea60d0b8 Switch prelude to a file and refactor lit testing to pass commands directly. (#966) 2021-12-03 14:23:13 -08:00
Geoff Romer f75b4d322f Handle use-before-declare in static name lookup (#967)
Also remove inheritance from NamedEntity for some classes that don't need it.
2021-12-03 13:52:31 -08:00
Jon Meow 3c76b0ab01 Test --trace directly (#933) 2021-12-02 13:54:35 -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 Romer be0c1e9da6 Move value-category checking to compile time (#960) 2021-11-29 17:07:50 -08:00
Geoff Romer b14c97d0d7 Clean up remaining uses of old spelling main (#959) 2021-11-24 14:32:39 -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 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
Chandler Carruth b990bab452 Fix main names that got past CI to fix tests. (#945)
#939 switch to `Main`, but #919 predated it and didn't get updated
before merging. Its tests passed on the PR branch as a consequence, but
failed when landed.

This just updates the test cases. Trivial fix forward.
2021-11-04 22:08:23 -07: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 6d822ababb Rename main to Main for Carbon style consistency (#939)
Main() is more consistency with Carbon's naming guidelines. C# offers some precedent: https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/main-command-line
2021-11-02 14:53:18 -07:00
Jon Meow eeff5dcdae Mark fnty as __Fn to reflect experimental state (#928)
The `__Fn` naming is intended to mirror things like `__Continuation`.

The reason for this path is because it's not clear this is the form we'll want, and I think experimental naming will help reflect that.
2021-11-01 10:56:04 -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
Jon Meow 91b99b1148 Test tuple indexing (#908) 2021-10-21 10:49:28 -07:00
Geoff Romer 7670f08200 Additional test cases for returning from match (#897)
Requested during review of #882
2021-10-18 15:41:33 -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
Geoff RomerandJon Meow c2140c6cb9 Check exhaustiveness of function-ending match statements (#882)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-10-13 16:04:43 -07:00
Geoff RomerandJon Meow bc5a42211b Support struct implicit conversions in type-checking (#870)
I should emphasize that I am **completely cheating** here. This PR does not add support for actually _performing_  implicit conversions at run time, because the AST doesn't yet contain the necessary type information. At run time, code like `var p: Point = {.x = 1, .y = 2};` directly initializes the name `p` with the _struct_ value `{.x = 1, .y = 2}`; no object of type `Point` is actually created. I'm only getting away with this because we don't yet have any tests that can tell the difference.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-10-12 10:27:20 -07:00
Geoff Romer d177a08e01 Make continuations consistently "shallow" (#874)
Prior to this change, `__await` would make a deep copy of the continuation stack, but shallow-copy the individual stack frames within it. As a result, continuations appeared to have shallow semantics so long as the continuation stack had only a single frame.

This change also removes an obsolete test from the brief period when we intended continuations to have deep-copy semantics, which has been passing basically by accident.
2021-10-11 14:57:09 -07:00
Geoff Romer d5eb9a72f9 Fix shadowing.carbon to test shadowing behavior. (#872) 2021-10-05 16:08:15 -07:00
Geoff Romer c4e40aaa86 Add support for struct types, following p0561. (#856) 2021-09-29 16:14:27 -07:00
Jon MeowandGeoff Romer 5aa958345b Implement auto return types, removing => returns (#850)
This implements #826, I think covering everything important there.

Regarding ReturnTypeContext, I broke that out because it started feeling like a significant number of args to be passing around, and I think this makes the association inside type checking clearer.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-09-27 11:17:24 -07:00
Jon Meow 395a331cde Reorganize tests into dirs and rename numbered tests (#825)
There's a small update to update_checks.py to handle the recursive directories. Also, I'm only using one level of nesting in this PR but really no reason we can't do more. I'm just not sure what clustering is best right now.

As a pattern, I'm trying to name all failing tests `fail_*.carbon`.
2021-09-15 16:37:34 -07:00
Jon Meow 5047cd6447 Switch executable_semantics to lit/filecheck tests. (#819) 2021-09-09 11:15:41 -07:00
Geoff Romer 0521890dbc Clarify the copy semantics of continuations (#816) 2021-09-08 11:36:16 -07:00
Jon Meow a03536a196 Add syntax for package and library (#792)
Doesn't add much logic, only takes advantage of parser structure for the ordering enforcement.

Note import_nonexistent tests should probably fail, but writing import tests needs a chain of functionality, and I figured I'd just start adding some to validate the syntax (not adding existent imports because that'd require multi-file structure).
2021-09-02 16:01:15 -07:00
Jon Meow 49013ae1cc Reject invalid string literal whitespace on unescape (#793)
This is based on discussion on #732: that we should probably parse the invalid whitespace, then reject it as part of string validation, rather than having different parses. I worry the question of "how is this parsed" may lead to subtly unexpected results if we aren't consistent, so I'm switching the logic from the lexer to the unescape library (and also adjusting the list of rejected whitespace).
2021-08-30 15:22:03 -07:00
Jon Meow 00779b60a8 Reformat lexer/parser tokens. (#772)
Related to discussion on #738, but also trying to standardize handling of everything and make placement of token spellings (`AND "and"`) consistently in lexer.lpp.

I could have gone the other direction, removing the list of things in lexer.lpp, but this feels like it does more to use the compiler to detect skew between lexer.lpp and parser.ypp.
2021-08-30 14:59:30 -07:00
Jon Meow ed2d171703 Configure reentrant bison/flex (#789)
Make the parser reentrant, working towards #769 and the ability to load other packages cleanly.
2021-08-30 14:23:16 -07:00
Jon Meow 3ec550a85f Replace int lines with file-associated SourceLocations (#779)
Most interesting changes should be ast/source_location.h and syntax/parse_and_lex_context.h
2021-08-26 10:07:16 -07:00
Jon Meow 367f9e4e94 Rename struct to class per #651 (#765) 2021-08-19 12:15:20 -07:00
Geoff RomerandJon Meow 7138ee400f Support complex type patterns in bindings. (#759)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-08-18 13:40:17 -07:00
Jon Meow 9a9ccd8b7d Exclude tab in string literals (#734)
Noticed this while working on #732, seemed easiest to split changes though.
2021-08-12 09:53:04 -07:00