Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C++ Overload Resolution Simulator: A First Step

C++ Overload Resolution Simulator: A First Step

My final project for “CSC 458: Symbolic Programming” (https://www.cdm.depaul.edu/academics/pages/courseinfo.aspx?CrseId=001494) at DePaul. A minimal C++ type system in Scheme.

Zhihao Yuan

March 14, 2016
Tweet

More Decks by Zhihao Yuan

Other Decks in Research

Transcript

  1. Try it out 1. Install CHICKEN Scheme http://wiki.call-cc.org/ 2. Get

    my package https://drive.google.com/file/d/0B5749ux4jowQV2tjWHFLa1hQ MXc/view?usp=sharing 3. unzip csc458-project.zip; cd csc458-project/ 4. chicken-install -s -test 5. rlwrap csi 6. (use cppors) ; inside the CHICKEN interpreter
  2. Define a C++ function type #;2> (decl-order (function 'void ((array

    'char 3 4)))) (void #((char |3| *))) ;–––––––––––––––-^^^-------- this is your favorite array-to-pointer adjustment in function signature
  3. More words about the functions (function R (Args...)) is a

    macro to create a function type, its return value can be passed around or be defined: #;4> (define f (function 'int ('int 'char))) and you can use ‘decl-order’ to show its readable form: #;5> (decl-order f) (int #(int char))
  4. Arrays #;8> (decl-order (array 'int 10 20 30)) (int |10|

    |20| |30|) #;9> (decl-order (array-of-unknown-bound 'int 10 20)) (int |10| |20| ||) #;10> (decl-order (array-of-unknown-bound 'int)) (int ||)
  5. Type predicates #;11> (is-array (array-of-unknown-bound 'int)) #t #;12> (is-array 'int)

    #f #;13> (is-pointer (decay (array-of-unknown-bound 'int))) #t
  6. Lots of stuff #;19> (decl-order (pointer-to (const 'int))) (int const

    *) #;20> (decl-order (lvalue (pointer-to (const 'int)))) (int const * &) #;21> (decl-order (function 'void ((lvalue (pointer-to (const 'int)))))) (void #((int const * &)))
  7. Serious? • Standard confirming (when we do, we do very

    precisely, but there are unimplemented parts) • ~140 tests • And prevent you from “breaking” the type system: #;2> (decl-order (array (function 'void ()) 3)) Error: uncaught exception: "no array of functions"
  8. Call it! #;2> (call (function 'void ()) 'char) too many

    arguments #f #;3> (call (function 'void ('int 'long)) 'char (const 'long)) #t
  9. And do it with your own types! (use coops coops-primitive-objects)

    (define-class std::string) (define-method (is-convertible (t <symbol>) (s std::string)) (or (equal? t (pointer-to 'char)) (equal? t (pointer-to (const 'char)))))
  10. There we go #;2> (call (function 'void (std::string)) 'char) `char'

    is not convertible to `#<coops standard-class `std::string'>' #f #;3> (call (function 'void (std::string)) (pointer-to 'char)) #t
  11. So what’s this project for? 1. Express C++ type system

    in a Lisp-like language 2. Make it programmable 3. So that we can experiment our ideas, build tools like to test the function calls, or do overload resolution, provide diagnosis, and allow users to programmably use these tools!
  12. And what I learned? • CHICKEN Scheme is great –

    healthy ecosystem plus ◦ Common Lisp keyword arguments (DSSSL) ◦ Common Lisp printf and format ◦ CLOS-like object system (COOPS) • Unit tests, modules and packaging (Egg) in CHICKEN • Pattern matching and varies SRFI libraries • Hygienic macros • Applying rules to tree-like structures • Some beauty in C++’s type system :)