nandtotetris / computer-visualizer

0 stars 0 forks source link

Access the feasibility of converting typescript classes into JS classes? #3

Closed henites72 closed 4 years ago

henites72 commented 4 years ago

In issue #2, we decided to use Javascript as a programming language for our system. But we implement all the abstractions in the system using Typescript, so we want to show a converted abstraction, completely written in Javascript?

mezzzi commented 4 years ago

@henites72 can you post here an example typescript class, a small one, if possible?

henites72 commented 4 years ago

@mezzzi should it be from the abstractions or some sample OOP code in Typescript?

mezzzi commented 4 years ago

I have found this class from @henites72's repository (book-projects). We can show the feasibility using this as a sample typescript class.

class SymbolTable {
  public table: Object;

  constructor() {
    this.intilalizeSymbolTable();
  }

  intilalizeSymbolTable() {
    this.table = {
      SP: 0,
      LCL: 1,
      ARG: 2,
      THIS: 3,
      THAT: 4,
      R0: 0,
      R1: 1,
      SCREEN: 16384,
      KBD: 24576
    };
  }

  addEntry(symbol: string, address: number) {
    this.table[symbol] = address;
  }

  contains(symbol: string): boolean {
    return this.table.hasOwnProperty(symbol);
  }

  getAddress(symbol: string): number {
    if (this.contains(symbol)) return this.table[symbol];
  }
}

export default SymbolTable;

The full code is available here: https://github.com/nandtotetris/henok-book-projects/blob/master/projects/06/src/symboltable.ts

henites72 commented 4 years ago

Great @mezzzi!

Buranch commented 4 years ago

I'm experimenting with this tool https://www.typescriptlang.org/play/ looks promising so far.

mikerezene commented 4 years ago

So are we going to write the whole code in Javascript and then we are going to find a tool to convert it to Typescript? is that what you meant?

mezzzi commented 4 years ago

No @mikereze, it is the other way around.

mezzzi commented 4 years ago

The sample typescript code posted above can be converted into the following javascript equivalent:

class SymbolTable {
    constructor() {
        this.intilalizeSymbolTable();
    }
    intilalizeSymbolTable() {
        this.table = {
            SP: 0,
            LCL: 1,
            ARG: 2,
            THIS: 3,
            THAT: 4,
            R0: 0,
            R1: 1,
            R2: 2,
            R3: 3,
            R4: 4,
            R5: 5,
            R6: 6,
            R7: 7,
            R8: 8,
            R9: 9,
            R10: 10,
            R11: 11,
            R12: 12,
            R13: 13,
            R14: 14,
            R15: 15,
            SCREEN: 16384,
            KBD: 24576
        };
    }
    addEntry(symbol, address) {
        this.table[symbol] = address;
    }
    contains(symbol) {
        return this.table.hasOwnProperty(symbol);
    }
    getAddress(symbol) {
        if (this.contains(symbol))
            return this.table[symbol];
    }
}
export default SymbolTable;

There is this tool that can help with the conversion: https://www.typescriptlang.org/play/index.html. Thanks @Buranch for suggesting it. But we shouldn't rely entirely on the tool. We need to double check each converted code to make sure there isn't error in the conversion.

Buranch commented 4 years ago

@mezzi yes I've already suggested a human review back in our slack discussion

On Friday, February 28, 2020, mezzzi notifications@github.com wrote:

The sample typescript code posted above can be converted into the following javascript equivalent:

class SymbolTable { constructor() { this.intilalizeSymbolTable(); } intilalizeSymbolTable() { this.table = { SP: 0, LCL: 1, ARG: 2, THIS: 3, THAT: 4, R0: 0, R1: 1, R2: 2, R3: 3, R4: 4, R5: 5, R6: 6, R7: 7, R8: 8, R9: 9, R10: 10, R11: 11, R12: 12, R13: 13, R14: 14, R15: 15, SCREEN: 16384, KBD: 24576 }; } addEntry(symbol, address) { this.table[symbol] = address; } contains(symbol) { return this.table.hasOwnProperty(symbol); } getAddress(symbol) { if (this.contains(symbol)) return this.table[symbol]; } } export default SymbolTable;

There is this tool that can help with the conversion: https://www.typescriptlang.org/play/index.html. Thanks @Buranch https://github.com/Buranch for suggesting it. But we shouldn't rely entirely on the tool. We need to double check each converted code to make sure there isn't error in the conversion.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nandtotetris/computer-visualizer/issues/3?email_source=notifications&email_token=AEI2ASTPX46F37H3GHKCQKDRFC4DBA5CNFSM4K43WLO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENHRPAA#issuecomment-592385920, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEI2ASU4INRMW4VP4AGQVZDRFC4DBANCNFSM4K43WLOQ .

mezzzi commented 4 years ago

This article (https://medium.com/javascript-scene/the-typescript-tax-132ff4cb175b) provides a detailed comparison, pro and con analysis, between js and typescript. And the author updates the scores to reflect latest developments in each ecosystem. As of now, the scores favors js . So that kind of puts us in the right track, at least thinking of long term benefits to be gained, as the project matures and scales.