Arc Forumnew | comments | leaders | submitlogin
2 points by fallintothis 5598 days ago | link | parent

You can build a new expand-symbolize (or whatever) and model it after expand-sexpr, since their functions are similar.

  $ diff -u old-ac.scm ac.scm
  --- old-ac.scm  2009-08-07 17:07:47.235041122 -0700
  +++ ac.scm      2009-08-07 17:07:52.789134895 -0700
  @@ -68,6 +68,7 @@                                  
          (or (let ((c (string-ref string i)))       
                (or (eqv? c #\:) (eqv? c #\~)        
                    (eqv? c #\&)                     
  +                 (eqv? c #\$)                     
                    ;(eqv? c #\_)                    
                    (eqv? c #\.)  (eqv? c #\!)))     
              (has-ssyntax-char? string (- i 1)))))  
  @@ -90,6 +91,7 @@                                  
     ((cond ((or (insym? #\: sym) (insym? #\~ sym)) expand-compose)
            ((or (insym? #\. sym) (insym? #\! sym)) expand-sexpr)  
            ((insym? #\& sym) expand-and)                          
  +         ((insym? #\$ sym) expand-symbolize)                    
        ;   ((insym? #\_ sym) expand-curry)                        
            (#t (error "Unknown ssyntax" sym)))                    
      sym))                                                        
  @@ -167,6 +169,14 @@                                             
                                   #t))                            
                  sym))                                            
                                                                   
  +(define (expand-symbolize sym)                                  
  +  (build-symbolize (reverse (tokens (lambda (c) (eqv? c #\$))   
  +                                    (symbol->chars sym)         
  +                                    '()                         
  +                                    '()                         
  +                                    #t))                        
  +                   sym))                                        
  +                                                                
   (define (build-sexpr toks orig)                                 
     (cond ((null? toks)                                           
            'get)                                                  
  @@ -180,6 +190,20 @@                                             
                          (err "Bad ssyntax" orig)                 
                          (chars->value (car toks))))))))          
                                                                   
  +(define (build-symbolize toks orig)
  +  (cond ((null? toks)
  +         'sym)
  +        ((null? (cdr toks))
  +         (chars->value (car toks)))
  +        (#t
  +         (list (build-symbolize (cddr toks) orig)
  +               (if (eqv? (cadr toks) #\$)
  +                   (list 'sym (chars->value (car toks)))
  +                   (if (eqv? (car toks) #\$)
  +                       (err "Bad ssyntax" orig)
  +                       (chars->value (car toks))))))))
  
   (define (insym? char sym) (member char (symbol->chars sym)))
  
   (define (symbol->chars x) (string->list (symbol->string x)))
Lightly tested:

  $ mzscheme -f as.scm                       
  Use (quit) to quit, (tl) to return here after an interrupt.      
  arc> $$x
  Error: "list->string: expects argument of type <list of character>; given #\\$"
  arc> (= tbl (table))
  #hash()
  arc> (= tbl$x '(1 2 3))
  Error: "reference to undefined identifier: _x"
  arc> (= x "key")
  "key"
  arc> (= tbl$x '(1 2 3))
  (1 2 3)
  arc> tbl
  #hash((key . (1 2 3 . nil)))
  arc> tbl$x
  (1 2 3)
  arc> tbl$x.0
  1
  arc> (tbl 'x)
  nil
  arc> (tbl 'key)
  (1 2 3)
  arc> $x
  key
  arc> $x$y
  Error: "reference to undefined identifier: _y"
  arc> (= y "abc")
  "abc"
  arc> $x$y
  Error: "Function call on inappropriate object key (abc)"
  arc> :a
  $ mzscheme -i -f as.scm
  Welcome to MzScheme v4.2.1 [3m], Copyright (c) 2004-2009 PLT Scheme Inc.
  Use (quit) to quit, (tl) to return here after an interrupt.
  arc> :a
  > (ac '$x$y '())
  (ar-funcall1 (ar-funcall1 _sym (ar-funcall1 _sym _x)) (ar-funcall1 _sym _y)) ; buggy?
Scheme syntax isn't all that different from Arc syntax. The main differences you see in this example are:

- Instead of

  (def f (x) body)
in Scheme it's

  (define (f x) body)
- Different names: Arc is = Scheme eqv?, Arc t & nil = Scheme #t & #f, Arc fn = Scheme lambda, etc.

- Scheme's cond is like Arc's if, but with extra parentheses.

  ; Arc
  (if a b
      c d
        e)

  ; Scheme
  (cond (a b)
        (c d)
        (#t e))
- You have access to a different set of library functions than the ones Arc provides. Most of what you need is defined in ac.scm. If you're unsure how it works, puzzle at the definition some.


2 points by thaddeus 5584 days ago | link

BTW I just got a chance to look at this tonight. I changed it a little so that $ would work with '!' and '.'

Again thanks for the help! T.

  (define (has-ssyntax-char? string i)
    (and (>= i 0)
         (or (let ((c (string-ref string i)))
               (or (eqv? c #\:) (eqv? c #\~) 
                   (eqv? c #\&)
                   (eqv? c #\$)                
                   ;(eqv? c #\_) 
                   (eqv? c #\.)  (eqv? c #\!)))
             (has-ssyntax-char? string (- i 1)))))


  (define (expand-ssyntax sym)
    ((cond ((or (insym? #\: sym) (insym? #\~ sym)) expand-compose)
           ((or (insym? #\. sym) (insym? #\! sym)(insym? #\$ sym)) expand-sexpr)
           ((insym? #\& sym) expand-and)
     ;     ((insym? #\_ sym) expand-curry)
           (#t (error "Unknown ssyntax" sym)))
     sym))

  (define (expand-sexpr sym)
    (build-sexpr (reverse (tokens (lambda (c) (or (eqv? c #\.) (eqv? c #\!)(eqv? c #\$)))
                                  (symbol->chars sym)
                                  '()
                                  '()
                                  #t))
                 sym))


  (define (build-sexpr toks orig)
    (cond ((null? toks)
           'get)
          ((null? (cdr toks))
           (chars->value (car toks)))
          (#t
           (list (build-sexpr (cddr toks) orig)
                 (cond ((eqv? (cadr toks) #\!)
                        (list 'quote (chars->value (car toks))))
                       ((eqv? (cadr toks) #\$)
                        (list 'sym (chars->value (car toks))))
                       ((or (eqv? (car toks) #\.) (eqv? (car toks) #\!)(car toks) #\$))
                         (err "Bad ssyntax" orig))
                       (#t (chars->value (car toks))))))))

-----

1 point by thaddeus 5598 days ago | link

That's great - thank you.

It'll take me a while to step through this, but I appreciate the guidance.

T.

-----