Git Product home page Git Product logo

rust-representer's Introduction

Rust Representer

A normalizing representer for Exercism's Rust track.

What's a Normalizing Representer?

A representer's job is to normalize some input code by stripping out and replacing any trivial details that introduce differences between students' submitted code. Comments, whitespace, and variable names, things that don't contribute to the overall logical flow and structure of the students' approach, are stripped out. In the case of variable names, these are replaced by a standard placeholder.

The ultimate purpose of the representer is to facilitate quicker response times from mentors by standardizing student implementations so that mentors can provide feedback on the approach the student took to solve the problem.

Example

Given an example submission for the two-fer exercise like the following:

fn twofer(name: &str) -> String {
    match name {
        "" => "One for you, one for me.".to_string(),
        // use the `format!` macro to return a formatted String
        _ => format!("One for {}, one for me.", name),
    }
}

The representer will return:

fn PLACEHOLDER_1(PLACEHOLDER_2: &str) -> String {
    match PLACEHOLDER_2 {
        "" => "One for you, one for me.".to_string(),
        _ => format!("One for {}, one for me.", PLACEHOLDER_2),
    }
}

Progress

Currently the following statement/expression types are visited by the representer:

  • let bindings
  • struct names
  • struct fields
  • enum names
  • enum variants
  • fn definitions
  • fn calls
  • method calls
  • const names
  • static names
  • union names
  • type aliases
  • match expressions
  • match arms
  • macro arguments
  • closure expressions
  • for loops
  • while loops
  • loops
  • if expressions
  • impl blocks
  • type annotations
  • if let bindings
  • user-defined types
  • user-defined traits
  • mod imports
  • output variable mappings to a JSON file

Design

The high-level steps the representer takes are as follows:

  1. It transforms the source code into an AST, stripping out comments in the process.
  2. From there, it traverses the AST, looking for identifiers.
  3. When it finds an identifier:
    • It checks whether the identifier is a Rust keyword (or any other sort of identifier that isn't actually being used as a variable/function name).
    • If the identifier isn't a keyword, it then checks if the identifier is one that has been encountered before.
      • If it is, then a placeholder for this identifier has already been generated and stored in a HashMap; the identifier is replaced with the placeholder.
      • If it isn't, then the placeholder needs to be generated and saved in the HashMap before the identifier is replaced by it.
  4. The transformed output is then put through another formatting pass.

rust-representer's People

Contributors

aurelienft avatar dependabot[bot] avatar efx avatar erikschierboom avatar exercism-bot avatar faisalafroz avatar ihid avatar kytrinyx avatar seanchen1991 avatar senekor avatar xuanwo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rust-representer's Issues

Replace items not just based on their name

I think this test case is inadvertantly exposing a sever limitation of the current representer. This function and its local variable have the same name and are assigned the same placeholder. That should not be the case however, as they refer to different items.

For running the representer in production, this means that two identical solutions may not receive the same representation, simply because two two different items shared or didn't share the same name.

Originally posted by @senekor in #30 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.