Commit Graph
65 Commits
Author SHA1 Message Date
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
Jon MeowandGeoff Romer 250ce4ab00 Add string parsing and a print builtin (#721)
It was in my mind to add String in order to support libraries in `package`.  `print` is added in order to have a String go to stdout. I've tried to do `print` in a way that won't be too hard to add other printable types, but it's probably also somewhat optional here -- that is, if desired, I could remove it. But it was a lot easier to doublecheck `\n` behavior with it, and I suspect it'll be helpful in other tests if it supports more value types.

On the side, this also fixes dereferencing in Pattern/Expression Print() calls, which I was noticing printing pointers instead of values. This may be another argument for moving away from passing pointers, since this seems to be a difficult-to-catch error.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-11 13:14:05 -07:00
Jon Meow 6164cdfd6d Verify that provision/omission of return types matches (#717)
This is finishing #678
2021-08-06 15:18:39 -07:00
Jon Meow 2e9e4f4cb3 Migrate remaining exits to FATAL_*_ERROR calls (#704)
Adds FATAL for things that are most likely programming errors in executable_semantics.
2021-08-06 13:06:25 -07:00
Jon MeowandGeoff Romer 2620ba066d Move Int parsing to i32 for #543 (#700)
Note this doesn't support other sizes or types, it just errors on them.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-05 09:34:12 -07:00
Jon Meow 6ae9cc3cf8 Add support for return; (#678)
This creates a ReturnExpression. A separate change should enforce that an implicit return of `()` is only allowed in functions that have an implicit return type of `()`, and I think the structure taken here should ease that.
2021-08-02 16:30:08 -07:00
jonmeow 91512e90e7 Fix tests affected by pattern change 2021-07-30 17:49:37 -07:00
Geoff RomerandJon Meow 6ac3adfa53 Factor out a Pattern sum type from Expression (#685)
`Pattern` is intended to pilot some changes I would like to apply to all our sum types:
- The alternatives are expressed as derived classes rather than members of a `std::variant`.
- The alternatives are classes in the [style guide sense](https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes), meaning they can have invariants, but can't have public data members.
- Creating an object is expressed using a constructor rather than a factory function.
- Accessing an alternative is expressed as a cast (using LLVM's RTTI system) rather than `std::get` or a `Get` method.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-07-30 12:24:12 -07:00
Jon Meow a930717202 Require braces on control flow statements (#683)
This implements #623
2021-07-28 10:55:14 -07:00
Jon MeowandChandler Carruth f0af3cb795 Switch from llvm::errs()+exit() to FATAL_USER_ERROR() stream-based handling. (#670)
This is intended to:

- Standardize use of line_num (and highlight where no line_num is available).
- Standardize indications of runtime/compilation errors.
- Make it incrementally easier to write new errors.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-07-28 10:47:33 -07:00
Geoff Romer fb0c0b63bf Fix match bug when no case matches (#677) 2021-07-27 11:14:10 -07:00
Jeremy G. Siek a1c164f598 added a test for definition-side checking with expected failure (#673) 2021-07-23 11:42:07 -04:00
Jeremy G. SiekandJon Meow 864b3bde02 Generic functions, first baby step (#658)
* generic functions: progress on parser and AST

* finished first baby step

* revisions based on reviews

* Update executable_semantics/interpreter/interpreter.cpp

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

* Symbol => VariableType

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-07-22 15:16:56 -04:00
Jon Meow 034f3600e3 Add a FatalUserError macro to help print user-caused errors. (#668) 2021-07-21 12:38:56 -07:00
Jon Meow 368fc0063c Handle invalid chars better, and do small cleanups around error output. (#657)
This adds a test for invalid characters (that would've failed before, because the printed char isn't escaped). Not sure if there's a good way to test the PrintDiagnostic code, as it appears to occur on bison parser errors, which I'm just not sure how to trigger.
2021-07-20 12:04:45 -07:00
Geoff RomerandJon Meow def98d1182 Add support for _ placeholder. (#661)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-07-19 10:33:06 -07:00
Richard SmithandGeoff Romer 89e21113c3 Add 3 '*' operators: one prefix, one infix, and one postfix, per #523. (#582)
The presence or absence of whitespace is used to determine which
operator is in use, following the rules described in #520.

Support for prefix * dereference operator follows #523.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-06-21 17:09:34 -07:00
Richard Smith 567b9bb5a4 Reverse 'type: identifier' to 'identifier: type' in executable
semantics.
2021-06-02 11:33:50 -07:00
Richard Smith da403db9ff Revert "Remove : from vars in exec semantics (#504)"
This reverts commit 05663633d3.
2021-06-02 11:33:50 -07:00
Jon Meow b1d11bae7e Fix 6c -> carbon ext (#536)
Chandler reminded me of this, the `.carbon` extension is noted here:
https://github.com/carbon-language/carbon-lang/tree/trunk/docs/design/code_and_name_organization#overview
2021-05-17 08:15:07 -07:00
Jon MeowandGeoff Romer 05663633d3 Remove : from vars in exec semantics (#504)
Going through tests, funptr1.6c is the main spot I see a real change, I added parens to get around an issue parsing fnty.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-05-06 14:21:31 -07:00
Jeremy G. Siek 56b47bcfa9 Type computation tests (#511)
* test of a little type computation

* another test and some bug fixes
2021-05-05 14:46:10 -04:00
Jeremy G. Siek 5946d9c033 Tuple restrictions (#486)
* implement tuple restrictions: (1) named fields after positional, (2) order matters

* after_named_member => seen_named_member
2021-04-22 17:51:43 -04:00
Jeremy G. SiekandGeoff Romer e89b8daad7 more tests of tuples, mixing positional and explicit field names (#480)
* more tests of tuples, especially mixing positional and explicit field names

* test of match with nested tuple

* Update executable_semantics/testdata/fun_named_params2.6c

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

Co-authored-by: Geoff Romer <gromer@google.com>
2021-04-21 17:23:16 -04:00
Jeremy G. SiekandGeoff Romer c1d651fc15 Fix equality for tuples (#446)
* fix tuple equality, added test cases

* added a comment to an old function

* Update executable_semantics/interpreter/value.cpp

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

* fix error in Geoffrey's edit

Co-authored-by: Geoff Romer <gromer@google.com>
2021-04-21 17:12:10 -04:00
Jeremy G. SiekandGeoff Romer 27fd9de5bf improved checking for liveness when reading and writing memory (#448)
* improved checking for liveness when reading and writing memory

* moving some functions to be methods of State

* finished moving functions into State

* Update executable_semantics/interpreter/interpreter.h

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

* moved some comments, other minor edits

Co-authored-by: Geoff Romer <gromer@google.com>
2021-04-21 14:10:42 -04:00
Geoff Romer cf7c97bf28 Unify tuple types with tuples-of-types in the interpreter (#442) 2021-04-12 16:13:00 -07:00
Geoff Romer 80f035874d Restructure handling of paren expressions (#417)
* Move nontrivial logic out of `parser.ypp` into `FieldList`, rename it to `ParenContents`, make it a class, and add tests
* Use a `FieldInitializer` struct instead of `std::pair<std::string, Expression*>` to represent the fields of a tuple
2021-04-05 10:49:25 -07:00
3fa72d2984 Experimental control-flow operator (#368)
* AST and syntax for delimited control

* stashing for later

* a little more progress

* progress on delimited continuations

* delimit, suspend, and resume implemented (draft)

* example that generates the natural numbers

* fixes

* tinkering

* changed demo to experimental

* comments and name changes

* describe delimited continuations in the README

* renamed Snapshot to Continuation, edits to comments

* Update executable_semantics/ast/statement.h

improve comment for MakeDelimitStmt

Co-authored-by: Dave Abrahams <dabrahams@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

remove snake_case

Co-authored-by: Dave Abrahams <dabrahams@google.com>

* edits to comments, change name of variable

* updates to handle review edits

* trailing whitespace

* fixes to delimited continuations, added more tests, also fixed assignment to do a copy

* improvements from Geoffrey

* new test from Geoffrey, fix for empty blocks

* more suggestions from Geoffrey

* more tests for delimited continuations, renaming some of them

* renamed test files

* improve a comment

* sketch of creating continuation

* initial implementation of shift/reset style continuations

* more documentation

* fix some camel case

* implemented deep copy of continuations, added a test case for it

* fixed a bug and got the recursive test case working

* removed __delimit, polished up __continuation

* back to shallow copy for continuations

* suggestions from Geoffrey

* removed structured binding (for now)

* Update executable_semantics/ast/expression.cpp

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

* responses to Geoffrey

Co-authored-by: Dave Abrahams <dabrahams@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2021-03-26 11:22:48 -04:00
Jeremy G. SiekandJon Meow 7c4fcd3fa5 turn off tracing by default, change golden (#381)
* turn off tracing by default, change golden

* Update executable_semantics/main.cpp

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

* Update executable_semantics/main.cpp

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

* merge with trunk, update goldens

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-03-16 09:03:10 -04:00