Commit Graph
98 Commits
Author SHA1 Message Date
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 25dce9fbcf Change Match clauses from pairs to classes (#858)
Updates style of the Match class at the same time.
2021-09-29 13:36:54 -07:00
Geoff Romer 532948d9a5 Miscellaneous tuple simplifications (#857)
- Drop unnecessary special-casing of 0-tuples.
- Drop unused constructor.
2021-09-27 12:10:16 -07:00
Jon Meow 04ab30f231 Make the AST mutable (#849)
The code is pretty intertwined: having the AST be truly mutable means (to me) changing parser.ypp to return non-const values, but then the way things are passed around between objects should be non-const (particularly an issue with lists), which then creates issues with construction of lists in the TypeChecker, which then TypeChecker needs to mostly be non-const.

Due to the difficulties in breaking this apart, whereas I'd previously considering refactoring accessor naming in the same PR, I've largely avoided doing so. The intent is then that this PR focuses mainly on const -> non-const AST behavior.

call_main moves out of interpreter.cpp so that interpreter.cpp can receive a fully const AST.
2021-09-27 10:57:31 -07:00
Jon Meow 721743bc58 Change Alternative to a class (#853)
Revives BisonWrap because this seems a reasonable use of it (avoiding the need to have an std::optional or pointer for Alternative, both of which I thought could be unclear about the intent).
2021-09-27 09:05:22 -07:00
Jon Meow 6ab2bff69c Switch FunctionDefinition to a class (#852)
Splitting out the task from #849
2021-09-27 09:05:04 -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 ff319d311a Switch Ptr to a C++ pointer using the nonnull attribute (#831)
The advantage is a C++ pointer is special, and this approach eliminates the Ptr class type that was causing problems in conversions. Attribute suggestion was courtesy of chandlerc. We're sticking with the Ptr name because it's shorter than Nonnull, and we're likely to keep this in lots of places.
2021-09-16 13:05:25 -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 RomerandJon Meow 33dd9a873d Debugging quality-of-life improvements (#810)
* Debugging quality-of-life improvements

- Use std::abort for `CHECK`/`FATAL` failures, which acts as a debugger breakpoint as well as automatically printing a stack trace.
- Re-enable printing continuations in `--trace` mode.
- Log the source location of each step in `--trace` mode.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-09-08 09:25:16 -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 Meow 5f8b231322 Switch Clause to Ptr (#800)
This is consistent with other lists, doesn't seem to benefit from being a Ptr.
2021-09-01 15:19:58 -07:00
Geoff Romer 3058fb99aa Clean up ManualTransition for match statements. (#773) 2021-08-30 16:47:26 -07:00
Jon Meow 32f5845e7b Refactor Interpreter/TypeChecker to classes to remove interpreter globals (#790)
Along with #789 this addresses most of #769 although global_arena is still a TODO (that's widespread and overlaps with other changes so I wanted to do it after these are in).
2021-08-30 15:21:40 -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
Geoff Romer 0ac220b30f Add equality comparisons to Ptr (#774) 2021-08-30 11:27:52 -07:00
Jon Meow fd89bcb4aa Convert Pattern and Expression to Ptr (#787)
Sorry about the big change, this is hard to split. ParenContents is used by both, templated, and expects the same pointer type. While I could duplicate ParenContents with some ExpressionParenContents or PatternParenContents, that seems a little kludgy versus a single large change handling both. The worst of it is that Expression is already pretty sweeping, Pattern is really just incrementally adding.

That said, I believe this includes a couple fixes I found with incorrect use of dyn_cast in typecheck.cpp (checked nullptr at the wrong step in 2 code locations). There's also a missing `*` in member.cpp this caught. I adjust passing of expressions for Return due to nullness (I felt adding another constructor was the best solution).

I add a `.Release()` to BisonWrap due to things like `$3.first` needing some way to work through BIsonWrap. I felt this was better than `operator->`, but feel free to comment if you prefer the other path (`.Release()` conveniently lets me do pair unwrapping, so it felt a better solution).

I do add a TODO to think about better Ptr-to-Ptr cast<> support too, though, as that doesn't work cleanly with LLVM's infra. But so far it seems to only come up in one spot, so I'm not prioritizing it.
2021-08-27 09:16:20 -07:00
Jon Meow e93a361032 Use Ptr for Member (#786) 2021-08-26 16:32:22 -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
Geoff Romer d71f5b1784 Express stack updates using return values. (#747)
This enables the interpreter logic to express its intent more directly, especially in the common cases, and enables us to get rid of ValAction. It's also a step toward simplifying and encapsulating `state->stack`.
2021-08-19 13:35:47 -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 Meow 925c60b669 Change Declaration passing to use Ptr (#758) 2021-08-19 11:33:32 -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 MeowandGeoff Romer c6ebe0db67 Add Ptr and RawNew for migration (#751)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-17 13:37:29 -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 d5564280ab Switch Statement to use inheritance+cast (#718) 2021-08-10 13:10:05 -07:00
Jon MeowandGeoff Romer dbcd6ad20d Switch Expression to use inheritance+cast (#712)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-06 15:43:25 -07:00
Jon Meow 4e0307efbc Switch Member to use inheritance+cast (#713) 2021-08-06 15:27:31 -07:00
Jon Meow 4b2346cfcd Switch Action to use inheritance+cast (#711) 2021-08-06 15:14:29 -07:00
Jon Meow c1148caf7d Switch Declaration to use inheritance+cast (#714) 2021-08-06 14:57:44 -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 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
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
Jon Meow 9829f188b7 Move all new's to global_arena, and remove ASAN disabling (#690)
Note this changes identifiers from char* to string to avoid malloc.

Fixes #580
2021-08-02 11:08:22 -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 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
Jon Meow edbc3f7716 Use llvm::ListSeparator for simpler separators (#671)
I was looking for something like this, I think it's a straightforward simplification for code. Internally, it handles skipping the separator on the first print.

See the bottom of: https://llvm.org/doxygen/StringExtras_8h_source.html
2021-07-23 08:39:58 -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 f3cbfc04c6 Move tracing_flag into a common directory (#669)
Intended to combine well with #668, so neither is alone.
2021-07-21 09:57:54 -07:00
Geoff Romer 08a2d44570 Simplify the API and implementation of Stack (#667) 2021-07-20 14:27:41 -07: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
Jon Meow 1ddb1a264a Restructure CHECK to provide a stream (#660) 2021-07-20 11:52:24 -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