Commit Graph
55 Commits
Author SHA1 Message Date
Jon Meow 92903afbd5 clang-tidy pass on executable_semantics (#963) 2021-12-02 12:21:18 -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 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 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
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 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
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 eeed6301d6 Refactor Value accessors (#892) 2021-10-18 10:50:56 -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
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
Jon Meow 70797e8bf8 Mass rename SourceLoc and Tag (#860)
This does a mass rename of:

-   `SourceLoc()` -> `source_loc()` for property naming
    - `loc` -> `source_loc_` for underscore+consistency
    - Generally changing function args to `source_loc` for consistency
-   `Tag()` -> `kind()` for property naming and `Kind` parity
    - `tag` -> `kind_` for underscore

Also renames `Pos` and `Results` on `Action`. These are a bit of an exception in that most base classes only have `Tag` and maybe `SourceLoc`, whereas `Action` has a little more. I felt okay having `source_loc()` and `kind()` on the base class where children do `Exp()` and the like, but it felt weird to me to mix it on the same class.

The reason for doing this cross-class in one PR is so that I can do it efficiently with a global replace in the codebase, rather than e.g. changing `Expression` but having to read through compiler errors to determine where it's calling `Expression`'s `Tag` versus a different `Tag`. The end result should be equivalent.
2021-09-29 16:52:12 -07:00
Geoff Romer c4e40aaa86 Add support for struct types, following p0561. (#856) 2021-09-29 16:14:27 -07:00
Jon Meow d157d96338 Migrate Declaration to newer property style, class-ify ClassDefinition (#859) 2021-09-29 14:38:18 -07:00
Jon Meow b1af08fed0 Rename ptr.h to nonnull.h (#839) 2021-09-20 15:15:30 -07:00
Jon Meow 56dc4ae375 Rename Ptr<T> to Nonnull<T*> (#832)
Note that ptr.h also includes an enable_if change, to help avoid https://bugs.llvm.org/show_bug.cgi?id=51881
2021-09-16 19:22:57 -07:00
Jon Meow 90f04700e2 Remove global_arena (#814)
With this, only main.cpp instantiates an arena. Maybe we'll want to split that up more later (e.g., so that the runtime interpreter uses its own arena), but given the intent to have type-checking update the AST, I thought this was a reasonable approach for now in order to avoid ownership complexities.

Fixes #769
2021-09-09 11:00:57 -07:00
Geoff Romer f931a8cead Replace all lists with vectors (#815) 2021-09-07 09:20:10 -07:00
Jon MeowandGeoff Romer 0601f5620b Switch Value to Ptr (#799)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-09-01 15:59:56 -07:00
Jon MeowandGeoff Romer 36ed79dc25 Convert Statement to use Ptr (#788)
Note this makes a few cases where the Statement was optional explicit (Block, If, Sequence). I do add a few CHECKs around where statements were optional and assumed but unverified.

I switch TypeCheckStmt to not take an optional Statement because I think it makes the call sites clearer in behavior. It's also a smaller change than the converse, because taking an optional Statement means the returned statement would also need to be optional. Arguably a wrapper for optional statements could be added, but this still seems cleaner to me, and there aren't that many cases of an optional statement.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-30 14:41:14 -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
Jon Meow 47325be112 Switch interpreter Stacks to Ptr (#763)
This also eliminates the ctad wrapper for Stack: I think the leaning is to remove it. It felt worth keeping the constructor because constructing with a single element is a common use-case.

Adds a single-argument constructor for Scope because the `std::list<std::string>()` is common, and eliding it is consistent with what we've done for things like tuples.

I was considering a vector constructor due to the double-Push on line 1139, but thought the Push() semantics may mean that it's better not to provide.
2021-08-19 11:34:03 -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 5749413b28 Add TupleValue::Empty for the common empty tuple case (#708) 2021-08-06 10:06:35 -07:00
Jon Meow 7e91fbc276 Switch Value to use the inherit/cast model (#703) 2021-08-05 08:34:08 -07:00
Geoff Romer a8f56797b8 Add Dump methods to printable types. (#681) 2021-07-29 14:17:35 -07: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 8fccecadeb Refactor output to be more streaming-focused. (#666)
- Switch code to llvm::raw_ostream as part of standardizing output forms.
    - Preferring llvm::raw_ostream over std::ostream because other tooling code should be expected to rely on llvm more closely, and an overall preference towards library consistency.
    - There are a couple spots in syntax/ that still use std streams, but I'd prefer to take a separate PR to see how best to address those.
    - std::boolalpha doesn't work with llvm, so I've implemented equivalent in a couple places (not enough that it felt like worth making a helper function).
- Implement Print(ostream) as consistently as we can, as an instance member.
    - This facilitates the use of the common/ostream.h template to provide operators.
    - Preferring this approach so that Print is easily accessible via gdb, per suggestion on #executable-semantics.
- Switch code currently calling `type->Print(ostream)` to instead do `ostream << *type`.
- Remove the unused `PrintTypeEnv`, nothing used it and the declaration didn't match the definition.
2021-07-20 13:16:48 -07:00
Geoff RomerandJon Meow d5124256a2 Split interpreter into smaller modules (#662)
- Move Heap and Frame/Scope to their own headers.
- Move some functions to more appropriate headers (e.g. CopyValue -> value.h).
- Define a separate Bazel rule for each header/cpp pair.
- Modify PrintValue to not print the frames of a ContinuationValue. This was necessary to break a dependency cycle between PrintValue, PrintFrame, and Action::Print.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-07-19 13:51:35 -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
Geoff Romer c3c89a77a4 Delete functions that are redundant with Get methods (#664)
This change assumes that the Carbon typechecker will ensure that the corresponding Get calls are safe.
2021-07-19 09:47:26 -07:00
Geoff Romer d2afd45214 Stop allocating sub-Values on the Heap. (#648)
This minimizes use of the Heap, and moves us toward not using it at compile time.
2021-07-15 14:18:15 -07:00
Jon Meow b03d3cdbb0 Use a lambda for tag's visit (#645)
Also shifts declaration order a little for [style](https://google.github.io/styleguide/cppguide.html#Declaration_Order); moving line_num down (data members after methods) and tag() down (factory functions before instance members).
2021-07-13 13:53:48 -07:00
Geoff Romer de05711966 Migrate Value to std::variant (#635)
Also:
- Switch data members, and associated parameters and return types to be values/references rather than pointers, where applicable.
- Move Expression's TagVisitor to anonymous namespace, to avoid name collision.
- Switch to trailing return syntax (per style guide).
2021-07-12 11:24:30 -07:00
Geoff Romer c96ee2aaac Remove VarTV (#617)
There are no tests for this kind of value, and it's not clear we'll need it.
2021-07-09 10:49:39 -07:00
Geoff Romer b3d4c56ef2 Rename "PatternVariable" names for clarity (#630)
Pretty much any names will be awkward, since these types represent cases where an `Expression` is not an expression, and a `Value` is not a value, but we can at least be more explicit about the fact that they represent bindings rather than variable usages.
2021-07-09 10:11:01 -07:00
Geoff Romer a80ce2c566 Consistent naming for Expression and Value. (#621)
- `Kind` enumerator names always match the corresponding factory function, accessor, and type names (if any).
- All abbreviations in those names are expanded.
- Those names always have a suffix to disambiguate expressions from values.

`VarTV` is excluded from these changes because there's a pending PR to remove it.
2021-07-07 11:32:33 -07:00
Geoff Romer c903eb3133 Remove Void (#540)
Implements resolution of #443.
2021-06-01 12:48:04 -07:00
Jeremy G. Siek 8924f5620b Private unions (#492)
* changed union of Statement to be private

* changed the union in Expression to be private

* changed union in Value to be private

* changed AST constructors to be static methods

* updates to syntax unit tests
2021-04-27 14:29:13 -04:00
Geoff Romer 1933cfeeeb Stop using std::pair in the implementation of tuples (#479) 2021-04-26 11:43:06 -07: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
Jeremy G. Siek 34f1a03f7b change all expression and statement pointers to be const (#449)
* change all expression and statement pointers to be const

* const in paren tests
2021-04-19 21:47:19 -04:00
Geoff Romer cf7c97bf28 Unify tuple types with tuples-of-types in the interpreter (#442) 2021-04-12 16:13:00 -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
Dave Abrahams dd4e37e761 const-ify Value*s in preparation for value semantic transformation (#409)
Making Values const allows us to separate the actual mutations (search for "*&" in this change) from places where the Value is effectively passed by-value.
2021-03-21 09:00:00 -07:00